【发布时间】:2014-07-15 12:14:05
【问题描述】:
我正在尝试使用以下 json 结构填充两个核心数据实体。
{
"fldrs": [
{
"FldName": "Messages",
"FldID": "Messages",
"PrntFldID": null,
"UnRdCunt": "0",
"Cunt": "0",
"IsDflt": "True",
"FldIconID": null,
"SubFldrs": [
{
"FldName": "Compose",
"FldID": "INDV9",
"PrntFldID": "Messages",
"UnRdCunt": "0",
"Cunt": "0",
"IsDflt": "False",
"FldIconID": null,
"SubFldrs": null,
"tasksLists": null
},
{
"FldName": "Inbox",
"FldID": "INDV10",
"PrntFldID": "Messages",
"UnRdCunt": "2",
"Cunt": "12",
"IsDflt": "True",
"FldIconID": null,
"SubFldrs": null,
"tasksLists": null
}
],
"tasksLists": null
},
{
"FldName": "My Tasks",
"FldID": "My Tasks",
"PrntFldID": null,
"UnRdCunt": "0",
"Cunt": "0",
"IsDflt": "False",
"FldIconID": null,
"SubFldrs": [
{
"FldName": "Pending",
"FldID": "INDV1",
"PrntFldID": "My Tasks",
"UnRdCunt": "9",
"Cunt": "9",
"IsDflt": "False",
"FldIconID": null,
"SubFldrs": null,
"tasksLists": null
}
],
"tasksLists": null
}
]
}
这里的“SubFldrs”是“flds”的子实体。
这是我正在使用的数据模型
以下是使用的模型类
@class ParentFolder;
@interface SubFolder : NSManagedObject
@property (nonatomic, retain) NSString * fldId;
@property (nonatomic, retain) NSString * fldName;
@property (nonatomic, retain) NSString * isDefault;
@property (nonatomic, retain) NSString * msgCount;
@property (nonatomic, retain) NSString * prntFolderId;
@property (nonatomic, retain) NSString * unreadCount;
@property (nonatomic, retain) ParentFolder *subFolder_folder;
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class SubFolder;
@interface ParentFolder : NSManagedObject
@property (nonatomic, retain) NSString * fldId;
@property (nonatomic, retain) NSString * fldName;
@property (nonatomic, retain) NSSet *folder_SubFolder;
@end
@interface ParentFolder (CoreDataGeneratedAccessors)
- (void)addFolder_SubFolderObject:(SubFolder *)value;
- (void)removeFolder_SubFolderObject:(SubFolder *)value;
- (void)addFolder_SubFolder:(NSSet *)values;
- (void)removeFolder_SubFolder:(NSSet *)values;
@end
Restkit Mapping 是通过以下代码完成的
RKObjectMapping* subFolderMapping = [RKObjectMapping mappingForClass:[SubFolder class] ];
[subFolderMapping addAttributeMappingsFromDictionary:@{
@"FldName": @"fldName",
@"FldID": @"fldId",
@"IsDflt": @"isDefault",
@"UnRdCunt": @"unreadCount",
@"Cunt": @"msgCount",
@"PrntFldID": @"prntFolderId"
}];
RKObjectMapping* folderMapping = [RKObjectMapping mappingForClass:[ParentFolder class] ];
[folderMapping addAttributeMappingsFromDictionary:@{
@"FldID": @"fldId",
@"FldName": @"fldName"
}];
[folderMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"subFolder"
toKeyPath:@"SubFldrs"
withMapping:subFolderMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:folderMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"fldrs"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
return responseDescriptor;
父实体 (ParentFolder) 正在按要求填充,但子实体 (SubFolder) 始终为空。 谁能帮帮我?
【问题讨论】: