2015-02-23 22 views
0

終止錯誤索引1超出界限的程序。其實我試圖解析csv文件中的列。我發現計算器類似這樣的問題,一些問題,但沒有答案的是有幫助的終止錯誤「對象1超出界限」的程序

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

之前它肯定會崩潰,所以在這裏你可以粘貼你從這裏你的應用程序出現錯誤?哪條確切的線引發異常? – ozgur 2015-02-23 05:57:37

+0

對不起,我沒有讓你@ozgur – 2015-02-23 05:58:53

+1

「索引1超越邊界」意味着你的數組最有可能只有一個元素:索引0(第一個元素)存在並且沒有錯誤發生;下一個循環(索引== 1),您嘗試訪問超出數組邊界並拋出異常。 – 2015-02-23 06:06:09

回答

0

檢查你的數組包含空指標,原因是要檢查有如果的[line componentsSeparatedByString:@","]幫助荷蘭國際集團任何索引是空的使用檢查從數組中刪除空指數以下概念

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