我正在尝试使用方法中调用的下一个方法为我的应用程序填充或播种某些项目didFinishLaunchingWithOptions
:
-(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];
for (int i = 0; i < [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 to file
if ([NSKeyedArchiver archiveRootObject:items toFile:itemsPath]) {
[ud setBool:YES forKey:@"MUserDefaultsSeedItems"];
}
}
}
该文件seed.plist
具有以下内容:
<?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>
问题在于,在执行 时NSArray *seedItems = [NSArray arrayWithContentsOfFile:filePath];
,它表示数组包含 3 个对象,但它们是Out of scope
and Summary Unavailable
。
请问有什么想法吗?
提前非常感谢!