我正在尝试将 JSON 响应映射到 的实例NSManagedObject
,但我不明白如何正确设置关系/连接。
我的课程是:
@interface CloudObject : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) CloudFolder *parent;
@end
@interface CloudFolder : CloudObject
@property (nonatomic, retain) NSSet *children;
@end
@interface CloudFile : CloudObject
@property (nonatomic, retain) NSString * mimeType;
@end
JSON 是这样的:
{
"name": "Folder1",
"is_dir": true,
"contents": [
{ "name": "Folder2", "is_dir": true }
{ "name": "File1", "is_dir": false, "mime_type": "audio/mpeg" }
]
}
我RKDynamicMapping
用来映射响应(根对象):
RKEntityMapping *fileMapping = [RKEntityMapping mappingForEntityForName:@"CloudFile" inManagedObjectStore:managedObjectStore];
[fileMapping addAttributeMappingsFromDictionary:@{@"name": @"name", @"mime_type": @"mimeType"}];
RKEntityMapping *folderMapping = [RKEntityMapping mappingForEntityForName:@"CloudFolder" inManagedObjectStore:managedObjectStore];
[folderMapping addAttributeMappingsFromDictionary:@{@"name": @"name"}];
RKDynamicMapping *objectMapping = [[RKDynamicMapping alloc] init];
[objectMapping addMatcher:[RKObjectMappingMatcher matcherWithKeyPath:@"is_dir" expectedValue:[NSNumber numberWithBool:NO] objectMapping:fileMapping]];
[objectMapping addMatcher:[RKObjectMappingMatcher matcherWithKeyPath:@"is_dir" expectedValue:[NSNumber numberWithBool:YES] objectMapping:folderMapping]];
但是我现在如何设置与@"contents"
(应该RKDynamicMapping
也是)和孩子/父母的关系/连接?(“内容”中包含的文件夹没有“内容”属性)