iOS数组越界,字典空值的防奔溃处理

https://github.com/MrLujh/SafeObject

    // 测试数组
    NSArray *arr = @[@"1",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2"];
    NSMutableArray *tableArray = [[NSMutableArray alloc] initWithArray:arr];
    NSLog(@"arr====%@   tableArray====%@",arr[100],tableArray[100]);
    NSLog(@"arr====%@   tableArray====%@",[arr objectAtIndex:100],[tableArray objectAtIndex:100]);

    // 测试字典
    NSDictionary *dict = @{@"name":@"",@"age":@"20"};
    NSMutableDictionary *tableDict = [[NSMutableDictionary alloc] initWithDictionary:dict];
    NSString *str= nil;
    NSString *str1 = nil;
    NSString *str2 = nil;
    NSArray *arr1 = nil;
    NSDictionary *dict1 = nil;
    [tableDict setValue:str forKey:str1];
    [tableDict setObject:str forKey:@"test"];
    [tableDict setObject:arr1 forKey:@"arr"];
    [tableDict setObject:dict1 forKey:str2];
    id ss = [tableDict objectForKey:@"arr"];
    if (ss == nil || [ss isKindOfClass:[NSNull class]]) {
        NSLog(@"kong");
    }
    NSLog(@"dict---name====%@   tableDict---age====%@     tableDict---test====%@   tableDict---str1====%@  tableDict----arr=====%@   dict----dict=====%@",[dict objectForKey:@"name"],[tableDict objectForKey:@"age"],[tableDict objectForKey:@"test"],[tableDict objectForKey:str1],tableDict[@"arr"],tableDict[str2]);
    NSLog(@"dict---name====%@   tableDict---age====%@",[dict objectForKey:@"name"],[tableDict objectForKey:@"age"]);
    
    // 测试字符串
    NSMutableString *tableString = [[NSMutableString alloc] initWithFormat:@"防止项目数组字典越界崩溃"];
    NSLog(@"%@",[tableString substringFromIndex:100]);

 

你可能感兴趣的:(iOS)