nsmutablearray - Interpreting a data stream in objective c -


i getting stream live feed. looks mutablearray:

  "columbia heights",     e04,     "cleveland park",     a05 

the first line name , second station code.

i need turn dictionary plist compliant.

so result in mind this:

name: "columbia heights", code:         e04, name:         "cleveland park", code:         a05 

what i've written far this:

nsmutabledictionary *stationdict = [nsmutabledictionary dictionarywithobjectsandkeys:@"name",@"name",@"code",@"code", nil];      int = 0;     (i=0; < counter; i++) {          // names , codes each of 3 fave stations         //[stationarray addobject:[mutablefavearray objectatindex:i]];          nsstring *tempname = [mutablefavearray[i] valueforkey:@"name"];         nsstring *tempcode = [mutablefavearray[i] valueforkey:@"code"];            [stationdict setobject:tempname forkey:@"name"];         [stationdict setobject:tempcode forkey:@"code"];      } 

i'm no longer getting error, output, same input.. i'm getting last record..

  "cleveland park",         a05 

updated code:

nsmutabledictionary *stationdict = [nsmutabledictionary dictionarywithobjectsandkeys:@"name",@"name",@"code",@"code", nil];      int = 0;     (i=0; < mutablefavearray.count; i++) {          nsstring *tempname = [mutablefavearray[i] valueforkey:@"name"];         nsstring *tempcode = [mutablefavearray[i] valueforkey:@"code"];          [stationdict setobject:tempname forkey:@"name"];         [stationdict setobject:tempcode forkey:@"code"];          [stationarray addobject:stationdict];      } 

this results in last dictionary entry being saved twice..

 {         code = a05;         name = "cleveland park";     },         {         code = a05;         name = "cleveland park";     } 

the problem you're creating single dictionary, stationdict, before for loop. hit for loop, changes values in stationdict, adds stationdict stationarray, changes values in stationdict again, adds same dictionary stationarray, , on. end array contains same object repeated mutablefavearray.count times.

the solution either create new dictionary @ beginning of body of for loop, or add copy of dictionary add array. so, either this:

for (i=0; < mutablefavearray.count; i++) {     stationdict = [nsmutabledictionary dictionary];     //...continue rest of code... 

or this:

    //...other loop code precedes this...     [stationarray addobject:[stationdict copy]]; } 

either approach ensures each thing add array dictionary that's distinct others you've added. refinement, speed code little using non-mutable dictionary. if have create new dictionary each time through loop, can create data want contain rather changing objects, don't need mutable. also, can declare loop variable inside for statement, ensures it'll go out of scope for terminates. also, can use objective-c's notation dictionaries:

nsmutablearray *stationarray = [nsmutablearray array]; (int i=0; < mutablefavearray.count; i++) {     [stationarray addobject:@{@"name":mutablefavearray[i][@"name"],                               @"code":mutablefavearray[i][@"code"]}]; } 

you can further abbreviate , maybe speed bit using fast enumeration:

nsmutablearray *stationarray = [nsmutablearray array]; (nsdictionary *fave in mutablefavearray) {     [stationarray addobject:@{@"name":fave[@"name"],                               @"code":fave[@"code"]}]; } 

or can let mutablefavearray enumerating you:

__block nsmutablearray *stationarray = [nsmutablearray array]; [mutablefavearray enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop))     {         [stationarray addobject:@{@"name":obj[@"name"],                                   @"code":obj[@"code"]}];     }]; 

that last 1 might bit faster, perhaps bit harder read.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -