iphone - Problem adding custom objects to Mutable Array -
quick question regarding array's in xcode. have ht efollowing code, supposed go through array of strings has got through php , json, , trun these strings custom object strings ivars object add object new array:
for (int = 0; i<[list count]; i++) { article *article = [[article alloc] init]; //creates custom object article.uid = [[list objectatindex:i] objectatindex:0]; article.title = [[list objectatindex:i] objectatindex:1]; //adds string ivars article.description = [[list objectatindex:i] objectatindex:2]; articlearray = [[nsmutablearray alloc] init]; //inits new array [articlearray addobject:article]; //should add object seems fail [article release]; //releases object nslog(@"%@", article.description); } nslog(@"%d", [articlearray count]); nslog([articlearray description]); }
the code return correct values using nslog(@"%@", article.description);
not correct length new array , adds 1 value array string article.description makes no sense me. list array contains 2 elements each of arrays in containing strings.
you're recreating articlearray in every loop. declarate outside, , work:
nsmutablearray *articlearray = [[nsmutablearray alloc] init]; //inits new array (int = 0; i<[list count]; i++) { article *article = [[article alloc] init]; //creates custom object article.uid = [[list objectatindex:i] objectatindex:0]; article.title = [[list objectatindex:i] objectatindex:1]; //adds string ivars article.description = [[list objectatindex:i] objectatindex:2]; [articlearray addobject:article]; //should add object seems fail [article release]; //releases object nslog(@"%@", article.description); } nslog(@"%d", [articlearray count]); nslog([articlearray description]); }
you may want use nicer for(nsarray *listelement in list) syntax instead.
Comments
Post a Comment