objective c - NSMutableArray being overwritten in for-loop -


i searched around , thought found answer doesn't appear working. trying make array of dictionaries later sorting, etc. can correct data dictionary object , key pairs, when try add dictionaries array previous array entries "overwritten". realize i'm doing wrong pointer nsarray, cannot straight. 1 post suggested moving instantiation of array out of loop, following 2 snippets produce identical results.

nsmutabledictionary *tempmatricesnw = [nsmutabledictionary dictionary];  nsuinteger middle = 0;  if (([factorsnw count] % 2) != 0) {     middle = (([factorsnw count]+1)/2)-1; }  nsmutablearray *tempmatricearraynw = [nsmutablearray array];  (nsuinteger = 1; < [factorsnw count]-1; i++) {      if (i == middle) {         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]] forkey:@"x"];         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]] forkey:@"y"];         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]-[[factorsnw objectatindex:i] intvalue]] forkey:@"diff"];         [tempmatricearraynw addobject:tempmatricesnw];     }     else {         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]] forkey:@"x"];         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:(([factorsnw count]-i)-1)] intvalue]] forkey:@"y"];         [tempmatricesnw setobject:[nsnumber numberwithint:abs([[factorsnw objectatindex:i] intvalue] - [[factorsnw objectatindex:(([factorsnw count]-i)-1)] intvalue])] forkey:@"diff"];         [tempmatricearraynw addobject:tempmatricesnw];     }      nslog(@"%@", tempmatricearraynw);  } 

i same result this...

nsmutabledictionary *tempmatricesnw = [nsmutabledictionary dictionary];  nsuinteger middle = 0;  if (([factorsnw count] % 2) != 0) {     middle = (([factorsnw count]+1)/2)-1; }  (nsuinteger = 1; < [factorsnw count]-1; i++) {      nsmutablearray *tempmatricearraynw = [nsmutablearray array];      if (i == middle) {         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]] forkey:@"x"];         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]] forkey:@"y"];         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]-[[factorsnw objectatindex:i] intvalue]] forkey:@"diff"];         [tempmatricearraynw addobject:tempmatricesnw];     }     else {         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]] forkey:@"x"];         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:(([factorsnw count]-i)-1)] intvalue]] forkey:@"y"];         [tempmatricesnw setobject:[nsnumber numberwithint:abs([[factorsnw objectatindex:i] intvalue] - [[factorsnw objectatindex:(([factorsnw count]-i)-1)] intvalue])] forkey:@"diff"];         [tempmatricearraynw addobject:tempmatricesnw];     }      nslog(@"%@", tempmatricearraynw);  } 

it doesn't seem matter put instantiation of nsmutablearray must doing else wrong...

thanks in advance help!

your previous entries overwritten because using same dictionary pointing same memory allocation. need create new dictionary in each loop of or after add tempmatricesnw dictionary array, instantiate again

nsuinteger middle = 0;  if (([factorsnw count] % 2) != 0) {     middle = (([factorsnw count]+1)/2)-1; }  nsmutablearray *tempmatricearraynw = [nsmutablearray array];  (nsuinteger = 1; < [factorsnw count]-1; i++) {      nsmutabledictionary *tempmatricesnw = [nsmutabledictionary dictionary];      if (i == middle) {         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]] forkey:@"x"];         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]] forkey:@"y"];         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]-[[factorsnw objectatindex:i] intvalue]] forkey:@"diff"];         [tempmatricearraynw addobject:tempmatricesnw];     }     else {         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:i] intvalue]] forkey:@"x"];         [tempmatricesnw setobject:[nsnumber numberwithint:[[factorsnw objectatindex:(([factorsnw count]-i)-1)] intvalue]] forkey:@"y"];         [tempmatricesnw setobject:[nsnumber numberwithint:abs([[factorsnw objectatindex:i] intvalue] - [[factorsnw objectatindex:(([factorsnw count]-i)-1)] intvalue])] forkey:@"diff"];         [tempmatricearraynw addobject:tempmatricesnw];     }      nslog(@"%@", tempmatricearraynw);  } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -