一个非常简单的问题,我找不到答案。我有我的核心数据实体子类 -Area
并且GPS
我正在使用来自 JSON 文件的数据设置它们。这似乎很好。但是如何设置两个新创建的实体对象之间的关系呢?
NSManagedObjectContext *context = [self managedObjectContext];
Area *area = [NSEntityDescription
insertNewObjectForEntityForName:@"Area"
inManagedObjectContext:context];
GPS *gps = [NSEntityDescription
insertNewObjectForEntityForName:@"GPS"
inManagedObjectContext:context];
NSDictionary *attributes = [[area entity] attributesByName];
for (NSString *attribute in attributes) {
for (NSDictionary * tempDict in jsonDict) {
id value = [tempDict objectForKey:attribute];
if ([value isEqual:[NSNull null]]) {
continue;
}
if ([attribute isEqualToString:@"latitude"] || [attribute isEqualToString:@"longtitude"]) {
[gps setValue:value forKey:attribute];
}
else {
[area setValue:value forKey:attribute];
}
}
[area setAreaGPS:gps]; // Set up the relationship
}
从Area
到的关系名称GPS
是areaGPS
错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-many relationship: property = "areaGPS"; desired type = NSSet; given type = GPS; value = <GPS: 0x369540>
我已经[area setAreaGPS:gps];
在几个示例中看到了这种语法,但显然我不明白如何正确使用它。