按照Ray Wenderlich 的新教程,我能够获取 JSON 数据并将其存储到核心数据中。不过,我很难理解如何处理 Core Data 中的关系。
这是我的数据模型:
这是我的 JSON:
{
"results": [
{
"name": "Trivia 1",
"objectId": "1000",
"createdAt": "2012-08-31 18:02:52.249 +0000",
"updatedAt": "2012-08-31 18:02:52.249 +0000",
"questions": [
{
"text": "Question 1"
},
{
"text": "Question 2"
},
{
"text": "Question 3"
}
]
}
]
}
最后,这里是我设置 managedObject 值的地方:
//Sets values for ManagedObject, also checks type
- (void)setValue:(id)value forKey:(NSString *)key forManagedObject:(NSManagedObject *)managedObject {
NSLog(@"TYPE: %@", [value class]);
//If managedObject key is "createdAt" or "updatedAt" format the date string to an nsdate
if ([key isEqualToString:@"createdAt"] || [key isEqualToString:@"updatedAt"]) {
NSDate *date = [self dateUsingStringFromAPI:value];
//Set date object to managedObject
[managedObject setValue:date forKey:key];
} else if ([value isKindOfClass:[NSArray class]]) { //<---This would be the array for the Relationship
//TODO: If it's a Dictionary/Array add logic here
for(NSDictionary *dict in value){
NSLog(@"QUESTION");
}
} else {
//Set managedObject's key to string
[managedObject setValue:value forKey:key];
}
}
我已经看过这个问题,但我真的很困惑如何将 Ray Wenderlich 示例中的各个部分连接在一起。任何帮助将不胜感激。