ios - How to handle nested property list -


good day!

i have problem doing nested property list in ios.

so, there 2 uitextfields accept random value , save property list. problem is, when input second value, overwrite first value inside of property list.

how handle, or write nested property list?

here's attempted code:

- (ibaction)writetoplist:(id)sender {     nslog(@"write.");      nsstring *finalpath = [[nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0] stringbyappendingpathcomponent:@"data.plist"];      nsmutabledictionary *fruitdictionary = [[nsmutabledictionary alloc] init];      nsstring *fruitname = [[nsstring alloc] initwithstring:[fruitnamefield text]];     nsstring *fruitdescription = [[nsstring alloc] initwithstring:[fruitdescriptionfield text]];      nsdictionary *fruitdetail = [nsdictionary dictionarywithobjects:[nsarray arraywithobjects:fruitname, fruitdescription, nil]                                                             forkeys:[nsarray arraywithobjects:@"fruit", @"description", nil]];      nsmutabledictionary *fruitplist = [[nsmutabledictionary alloc] initwithcontentsoffile:finalpath];     nsmutablearray *fruitarray = [fruitplist objectforkey:fruitname];      if (fruitarray == nil) {         fruitarray = [[nsmutablearray alloc] init];     }      [fruitdictionary setobject:fruitdetail forkey:fruitname];      [fruitarray addobject:fruitdictionary];     [fruitarray writetofile:finalpath atomically:yes];      [[self presentingviewcontroller] dismissviewcontrolleranimated:yes                                                         completion:nil]; } 

the output is:

<plist version="1.0"> <array>     <dict>         <key>apple</key>         <dict>             <key>description</key>             <string>red</string>             <key>fruit</key>             <string>apple</string>         </dict>     </dict> </array> </plist> 

what want happen is:

<plist version="1.0"> <array>     <dict>         <key>apple</key>         <dict>             <key>description</key>             <string>red</string>             <key>fruit</key>             <string>apple</string>         </dict>     </dict>     <dict>         <key>banana</key>         <dict>             <key>description</key>             <string>yellow</string>             <key>fruit</key>             <string>banana</string>         </dict>     </dict> </array> </plist> 

by way, code acceptable or not? mean, there way shorten it?

so played around code , find out problems.

here wrote , tested. working.

  nsstring *finalpath = [[nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0] stringbyappendingpathcomponent:@"data.plist"];   nsmutabledictionary *fruitdictionary = [[nsmutabledictionary alloc] init];   nsstring *fruitname = fname;   nsstring *fruitdescription = fdetail;   nsdictionary *fruitdetail = [nsdictionary dictionarywithobjects:[nsarray arraywithobjects:fruitname, fruitdescription, nil]                                                           forkeys:[nsarray arraywithobjects:@"fruit", @"description", nil]];   nsmutablearray *fruitplist = [[nsmutablearray alloc] initwithcontentsoffile:finalpath];   if (fruitplist == nil) {         fruitplist = [[nsmutablearray alloc] init];     }   [fruitdictionary setobject:fruitdetail forkey:fruitname];   [fruitplist addobject:fruitdictionary];   [fruitplist writetofile:finalpath atomically:yes]; 

explanation: getting inner dictionary in code sroot of plist. instead @ root want have nsarray. thats why, this:

nsmutablearray *fruitplist = [[nsmutablearray alloc] initwithcontentsoffile:finalpath]; 

in code fname , fdetail variables passing.
hope helps!!


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -