nsarray - iOS: Seeding an app with items from a plist -
i'm trying populate or seed app items, using next method, called in didfinishlaunchingwithoptions method:
-(void)seeditems { nsuserdefaults *ud = [nsuserdefaults standarduserdefaults]; if (![ud boolforkey:@"muserdefaultsseeditems"]) { // load seed items nsstring *filepath = [[nsbundle mainbundle] pathforresource:@"seed" oftype:@"plist"]; nsarray *seeditems = [nsarray arraywithcontentsoffile:filepath]; nsmutablearray *items = [nsmutablearray array]; (int = 0; < [seeditems count]; i++) { nsdictionary *seeditem = [items objectatindex:i]; mshoppingitem *shoppingitem = [mshoppingitem createshoppingitemwithname:[seeditem objectforkey:@"name"] andprice:[[seeditem objectforkey:@"price"] floatvalue]]; [items addobject:shoppingitem]; } // items path nsstring *itemspath = [[self documentsdirectory] stringbyappendingpathcomponent:@"items.plist"]; // write file if ([nskeyedarchiver archiverootobject:items tofile:itemspath]) { [ud setbool:yes forkey:@"muserdefaultsseeditems"]; } } } the file seed.plisthas following content:
<?xml version="1.0" encoding="utf-8"?> <!doctype plist public "-//apple//dtd plist 1.0//en" "http://www.apple.com/dtds/propertylist-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>name</key> <string>naranjas</string> <key>price</key> <integer>0</integer> </dict> <dict> <key>name</key> <string>peras</string> <key>price</key> <integer>0</integer> </dict> <dict> <key>name</key> <string>manzanas</string> <key>price</key> <integer>0</integer> </dict> </array> </plist> the problem that when doing nsarray *seeditems = [nsarray arraywithcontentsoffile:filepath];, says array contains 3 objects, they're out of scope , summary unavailable.
any idea, please?
thanks lot in advance!
after have filepath, check exists using nsfilemanager. if doesn't exist, check file added xcode , set part of app target appears in 'copy bundle resources' build phase. can check target membership selecting seed file in xcode , viewing 'target membership' in utilities inspector.
your update question indicates array loaded, in debugger listed not available. indicates array has been released (because isn't used anymore). indicates typo. looking @ code more closely, see:
nsdictionary *seeditem = [items objectatindex:i]; which should be:
nsdictionary *seeditem = [seeditems objectatindex:i]; i guess seeing crash?
Comments
Post a Comment