终止程序错误"对象1超出界限"

时间:2015-02-23 05:44:01

标签: ios objective-c arrays for-loop

使用超出边界的错误索引1终止程序。实际上我试图解析csv文件中的列。我发现很少有关于stackoverflow的问题与此问题类似,但答案都没有帮助

 NSMutableArray *lines = [sourceFileString componentsSeparatedByString:@"\n"].mutableCopy;
 NSArray *keys = [lines.firstObject componentsSeparatedByString:@","];
 NSMutableArray *result = [NSMutableArray array];
 for (int i = 0; i<keys.count; i++) {
     [result addObject:@{keys[i] : [NSMutableArray array]}];
     }
 [lines removeObjectAtIndex:0];

for (NSString *line in lines) {
// Get a list of all values
NSArray *columns = [line componentsSeparatedByString:@","];

  // Insert the value into the array of the proper key
  NSMutableArray *values = result[1][keys[1]];
  [values addObject:columns[1]];
  NSLog(@"values %@",values);

}

1 个答案:

答案 0 :(得分:0)

检查你的数组是否包含空索引,原因是你在[line componentsSeparatedByString:@","]的帮助下检查是否有任何索引为空它肯定会崩溃,所以在这里检查之前从数组中删除空索引概念

for (int j=[lines count]-1; j>=0; j--)
    if ([[lines objectAtIndex:j] length] == 0)
        [lines removeObjectAtIndex:j];