答えが見つからない本当に簡単な質問。Core Dataエンティティをサブクラス化Area
しGPS
ており、JSONファイルのデータを使用してそれらを設定しています。それは問題ないようです。しかし、新しく作成された2つのエンティティオブジェクト間の関係を設定するにはどうすればよいですか?
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];
いくつかの例でこの構文を見てきましたが、どうやらそれを正しく使用する方法を理解していないようです。