in application implemented several tableviewcontrollers display nsobject each cell. every tableviewcontroller has different "filter" applied array every table displays specific group of these objects. in viewdidload of each tableviewcontroller load array nsuserdefaults, filter it, , display it. works fine, using nscoder objects encoded/decoded storing. developing purposes only, create each object programmatically (initwithname:(nsstring*)name etc.), add objects array , store it. sure not final solution. question: searching way implement array of objects without creating each object hand in code.the number of objects fixed und content can not change. need loaded. add: maybe in future updates add new items list. how can it?
so implemented @luke smith said , works perfectly! thank you!
nsstring *plistpath = [[nsbundle mainbundle] pathforresource:@"myplist" oftype:@"plist"]; nsarray *allelements = [[nsarray alloc] initwithcontentsoffile:plistpath]; (nsdictionary *newobject in allelements){ object *tempobject = [[object alloc] init]; tempobject.type = [newobject objectforkey:@"type"]; tempobject.isfavorite = [newobject objectforkey:@"isfavorite"]; [elements addobject:tempobject];//mutablearray storing elements } but did little bit of brain-storming came conclusion need edit entries. unfortunately since .plist in mainbundle not editable... possible store .plist in documents directory beginning ( maybe in xcode )? .plist :
<plist version="1.0"> <array> <dict> <key>isfavorite</key> <false/> <key>name</key> <string>obj1</string> </dict><dict> <key>isfavorite</key> <true/> <key>name</key> <string>obj2</string> </dict> </array> </plist> so when user pushes specific button, want change isfavorite value in .plist file item.
look @ loading values plist.
you need create new plist file in project , add properties (looks bit json editor - easy understand)
in code, can load plist memory using line of code :
nsstring *pathtomyplist = [[nsbundle mainbundle] pathforresource:@"myplist" oftype:@"plist"]; nsdictionary *myplistcontents = [[nsdictionary alloc] initwithcontentsoffile:pathtomyplist]; you have nsdictionary containing keys , values specified in plist. can loop through these keys , values initialize objects.
Comments
Post a Comment