我有一个值的属性列表需要读入Core Data。我可以读取“顶层”,但是每个项目除了字符串和数字外,还包含字典。我在获取正确的语法以读取这些方面遇到了问题。
Garden和Plant是我的两个Core Data实体。 Garden和Plant都是包含有关其自身数据的NSDictionary。花园可以包含许多植物,特定的植物可以在许多花园中,因此存在多对多的关系。
这部分有效:
我使用花园的属性列表字典来填充我的核心数据库,第一次运行时调用,如下所示:
Garden和Plant是我的两个Core Data实体。 Garden和Plant都是包含有关其自身数据的NSDictionary。花园可以包含许多植物,特定的植物可以在许多花园中,因此存在多对多的关系。
这部分有效:
我使用花园的属性列表字典来填充我的核心数据库,第一次运行时调用,如下所示:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:@"firstRun"])
{
[defaults setObject:[NSDate date] forKey:@"firstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self loadGardenDataFromPlistDictionary];
}
这很有效。将各种属性列表值加载到核心数据的第一级也是如此。但我在加载包含在“garden”字典内部的“plants”字典中的数据时遇到了麻烦……它是2级深度的。换句话说,名称、详细信息和注释都可以加载……但植物不能。以下是我的代码:
-(void) loadGardenDataFromPlistDictionary{
// build path to plist; check Documents first, then Bundle.
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"defaultGardenDictionary.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"defaultGardenDictionary" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property list into dictionary object
NSDictionary *plistDictionary = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!plistDictionary)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
NSManagedObjectContext *context = self.managedObjectContext;
[plistDictionary enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id key, id object, BOOL *stop) {
NSManagedObject *gardenManObj = [NSEntityDescription insertNewObjectForEntityForName:@"Garden" inManagedObjectContext:context];
[gardenManObj setValue:[thisGardenDictionary objectForKey:@"name"] forKey:@"name"];
[gardenManObj setValue:[thisGardenDictionary objectForKey:@"detail"] forKey:@"detail"];
[gardenManObj setValue:[thisGardenDictionary objectForKey:@"notes"] forKey:@"notes"];
[gardenManObj setValue:[thisGardenDictionary objectForKey:@"rating"] forKey:@"rating"];
NSDictionary *thisGardenPlantsDictionary = [thisGardenDictionary objectForKey:@"plants"];
NSLog(@"thisGardenPlantsDictionary contains %i dictionaries.", [thisGardenPlantsDictionary count]);
//The trace statement above shows the correct number of plants in each garden dictionary. oooh... so tantalizingly close!
//However, the next item is the dictionary of plants in this garden. It comes up blank.
//I don't understand the syntax to load that dictionary of plants into its corresponding dictionary of plants within a garden in core data.
//I deleted most of the things I've tried and failed with so far. Here's my last attempt:
// the obvious and parallel:
[gardenManObj setValue:[thisGardenDictionary objectForKey:@"plants"] forKey:@"plants"];
//did not work; xCode compiler objected to "incompatible pointer types."
//the compiler seemed to want an NSSet, so I tried:
NSSet *thisGardenPlantsSet = [thisGardenDictionary mutableSetValueForKey:@"steps"];
NSLog(@"thisGardenPlantsSet contains %i sets.", [thisGardenPlantsSet count]);
[gardenManObj setValue:thisGardenPlantsSet forKey:@"steps"];
//the compiler was fooled, but the setValue crashed at runtime.
}];
我需要什么?
我错过了什么?