2011-12-26 63 views
0

在數據讀取正確地創建一個數組的數組到目前爲止,我有:如何通過從文件

//read in data from CSV file and separate it into 'row' strings: 
// where dataString is simply a CSV file with lines of CSV data 
// 31 lines with 9 integers in each line 
NSArray *containerArray = [dataString componentsSeparatedByString:@"\n"]; 
NSArray *rowTemp; //local variable just for my sake 
NSMutableArray *tableArray;//mutable array to hold the row arrays 

//For each index of containerArray: 
//take the string object (string of CSV data) and then, 
//create an array of strings to be added into the final tableArray 

    for (int i = 0; i < [containerArray count]; i++) { 
    rowTemp = [[containerArray objectAtIndex:i] componentsSeparatedByString:@","]; 
    [tableArray addObject:rowTemp];   
    } 

然後,當我嘗試以下方法,它返回:(空)

NSLog(@"Row 6, cell 1 is %@", [[tableArray objectAtIndex:5] objectAtIndex:0]); 

有任何想法嗎?有沒有更好的辦法?

僅供參考這個數據是靜態的,不太可能改變。任何方式來創建和填充靜態數組,而不是使用可變數組,將不勝感激。

在此先感謝。

+1

我剛剛得到了答案!從iPhoneSDK論壇的一些幫助,我沒有做的是實際上創建tableArray。我在上面的代碼中做的只是創建變量。 NSMutableArray * tableArray; 並沒有真正創建我想要的可變數組。相反,我應該做的是: NSMutableArray * tableArray = [NSMutableArray arrayWithCapacity:[containerArray count]]; 它像一個魅力工作。 – 2011-12-26 03:11:06

回答

0

我剛剛得到了答案!從iPhoneSDK論壇的一些幫助,我沒有做的是實際上創建tableArray。我在上面的代碼中做的只是創建變量。 NSMutableArray * tableArray;並沒有真正創建我想要的可變數組。相反,我應該做的是:NSMutableArray * tableArray = [NSMutableArray arrayWithCapacity:[containerArray count]];其中像魅力工作。