2011-07-15 4 views
0

私はCSVファイルを読みましたが、今は文字列を解析して、要素を2D NSMutableArrayに書き出しています。NSMutableArrayは問題を書いていますか?

私は2D配列に要素を書き出しています。NSLogは期待される要素を出力します。

しかし、すべてのファイルを解析して2D配列に書き込むと、NSLogはその行の各列の最後の要素を各行に表示します。

- 行のすべての要素が置き換えられた場合 ???なぜ、...高度に深刻ないくつかは、私はそこに潜む問題とは別に、あなたの例で見ているスラッシュで

for (int i = 1; i < iM; i++) {//was a 1 to pass over the header 
    //get the row and break up into columns 
    nsmarrDummy = [NSMutableArray arrayWithArray:[[nsmarrRow objectAtIndex: i] componentsSeparatedByString: nsstrColParse]]; 


    //write each item to the proper column in the 2d array at the given row 
    for (int j = 0; j < iN; j++) {//<<-- 
     [[nsmarrData objectAtIndex:j] replaceObjectAtIndex:i-1 withObject: [nsmarrDummy objectAtIndex:j]]; 
     NSLog(@"i:%d j:%d item:%@", i, j, [[nsmarrData objectAtIndex:j] objectAtIndex:i-1]); 
    } 

} 
//all the following are the same value, but doing the NSLog as it was writing was correct. 
NSLog(@"FINAL: i:%d j:%d item:%@", 0, 4, [[nsmarrData objectAtIndex:4] objectAtIndex:0]); 
NSLog(@"FINAL: i:%d j:%d item:%@", 0, 5, [[nsmarrData objectAtIndex:5] objectAtIndex:0]); 
NSLog(@"FINAL: i:%d j:%d item:%@", 0, 6, [[nsmarrData objectAtIndex:6] objectAtIndex:0]); 

答えて

1

をお願いします。ココアはあなたのために多くの仕事をすることができます。それは雷の魅力を持っています。

// Get the contents of the file. 
// Real apps never ignore their errors or potential nil results. 
// For this example, assume path exists. 

NSString *fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL]; 

NSMutableArray *nsmarrData = [NSMutableArray array]; 

NSArray *lines = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; 
for (NSString *line in lines) 
{ 
    // Guard against the last line being only a return. 

    if ([line length] > 0) 
    { 
     NSArray *tokens = [line componentsSeparatedByString:@","]; // assuming no space as well 
     [nsmarrData addObject:tokens]; 
    } 
} 

これは、NSMutableArrayを各行に埋め込んで作成します。各行にNSMutableArrayが必要ですか?問題ない。代わりに:

[nsmarrData addObject:tokens]; 

あなたが使用することができます。もちろん

[nsmarrData addObject:[NSMutableArray arrayWithArray:tokens]]; 

を、これのどれもが、ライン上の項目の数を変化させるため占めません。後でそのことに注意する必要があります。

あなたの努力の中であなたに幸運。

関連する問題