objective c - Why does replaceObjectAtIndex depend on whether or not I use a new definition in the loop? -


i have 2 codes. not working following:

nsmutablearray *tmparray = [[nsmutablearray alloc] init]; (int i=0; i<[datasetarray count]; i++) {     tmparray = (nsmutablearray *) [datasetarray objectatindex:i];     // or use: tmparray = datasetarray[i]          ... doing stuff     [tmparray replaceobjectatindex:0 withobject:tmpstr]; } 

while works:

for (int i=0; i<[datasetarray count]; i++) {     nsmutablearray *tmparray = [[nsmutablearray alloc] initwitharray:[datasetarray objectatindex:i]];          ... doing stuff     [tmparray replaceobjectatindex:0 withobject:tmpstr]; } 

two questions:

  1. the first code doesn't yield nsmutablearray. why? declare above.
  2. is there better way obtain same result. dislike defining variables in loop. makes code unreadable.

--- edit:

here full code:

datatypes are:

datasetarray: nsmutablearray. however, contents (i.e. datasetarray[i]) nsarrays (i read them program excel file).

nsstring       *tmpstr   = [[nsstring alloc] init]; (int i=0; i<[datasetarray count]; i++) {     nsmutablearray *tmparray = [[nsmutablearray alloc] initwitharray:[datasetarray objectatindex:i]];     (int j=0; j<[tmparray count]; j++) {         if ( [datasetarray[0][j] isequaltostring:@"number"] ) {continue;}         tmpstr = (nsstring *) [tmparray objectatindex:j];          // replace single backslash double-backslash:         tmpstr = [tmpstr stringbyreplacingoccurrencesofstring:@"\\" withstring:@"\\\\"];          // replace first dollar sign "<p>\\[" , second "\\]</p>"         // use methode defined in nsstring+extension         tmpstr = [tmpstr replacetexformulasigns:tmpstr];          //nslog(@"j=%d", j);         //nslog(@"tmparray of type: %@", [tmparray class]);         //nslog(@" tmpstr of type: %@", [tmpstr class]);         [tmparray replaceobjectatindex:j withobject:tmpstr];     }     [datasetarray replaceobjectatindex:i withobject:tmparray]; } 

so if use suggestion, still facing same problem inner array.

the first code doesn't yield nsmutablearray. why? declare above.

the declaration of reference variable tmparray not change type of referred object. still (immutable) array.

the creation of mutable array @ beginning of first snippet without meaning, because reference overridden.

is there better way obtain same result. dislike defining variables in loop. makes code unreadable.

yes. second example works in way, different. (it creates new array single item in it. no, that's not true. shouldn't compile @ all.)

you should do:

nsmutablearray *tmparray = [datasetarray mutablecopy]; (int i=0; i<[datasetarray count]; i++)  {   …   [tmparray replaceobjectatindex:i withobject:tmpstr]; } 

you should really additional knowledge objects , object references.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -