objective c - NSMutableArray writing issue? -



objective c - NSMutableArray writing issue? -

i read in csv file , parsing string , writing element 2d nsmutablearray.

as writing elements 2d array nslog outputs expected element.

but when done parsing , writing whole file 2d array, nslog shows lastly element @ each row each column @ row.

--as if every element in row replaced. ??? why, give thanks in advanced...

for (int = 1; < im; i++) {//was 1 pass on header //get row , break columns nsmarrdummy = [nsmutablearray arraywitharray:[[nsmarrrow objectatindex: i] componentsseparatedbystring: nsstrcolparse]]; //write each item proper column in 2d array @ given row (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 next same value, doing nslog writing 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]);

that serious thrash i'm seeing in example, aside problem lurking there. cocoa can much of work you. it's got thunder appeal. allow on side:

// contents of file. // real apps never ignore errors or potential nil results. // example, assume path exists. nsstring *filecontents = [nsstring stringwithcontentsoffile:path encoding:nsutf8stringencoding error:null]; nsmutablearray *nsmarrdata = [nsmutablearray array]; nsarray *lines = [filecontents componentsseparatedbycharactersinset:[nscharacterset newlinecharacterset]]; (nsstring *line in lines) { // guard against lastly line beingness return. if ([line length] > 0) { nsarray *tokens = [line componentsseparatedbystring:@","]; // assuming no space [nsmarrdata addobject:tokens]; } }

this produces nsmutablearray filled nsarrays each row. need nsmutablearray each row? no problem. instead of:

[nsmarrdata addobject:tokens];

you can use:

[nsmarrdata addobject:[nsmutablearray arraywitharray:tokens]];

of course, none of accounts varying numbers of items on line; you'll need beware of later.

good luck in endeavors.

objective-c cocoa-touch

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -