我在核心数据中有一个名为“类别”的实体,它有 3 个属性。
- parent_id
- 类别ID
- 分类名称
现在在根字典下的 CategoriesFile.plist 中,我创建了 3 个名为
- parent_id
- 类别ID
- 分类名称
我已经在这些数组中填充了适当的数据。
现在我编写了以下代码来插入数据
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"CategoriesFile.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"CategoriesFile" 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 *thisGardenDictionary = [[NSDictionary alloc] init];
NSDictionary *plistDictionary = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!plistDictionary)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
[plistDictionary enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id key, id object, BOOL *stop) {
NSManagedObject *manObj = [NSEntityDescription insertNewObjectForEntityForName:@"Categories" inManagedObjectContext:context];
[manObj setValue:[thisGardenDictionary objectForKey:@"category_id"] forKey:@"category_id"];
[manObj setValue:[thisGardenDictionary objectForKey:@"parent_id"] forKey:@"parent_id"];
[manObj setValue:[thisGardenDictionary objectForKey:@"category_name"] forKey:@"category_name"];
}];
NSError *error;
if(![context save:&error])
{
NSLog(@"Whoops, couldn't save %@",[error localizedDescription]);
}
但我得到空白的结果。
有人可以说我错过了什么吗?