我有一个名为Client的对象,看起来像这样。
@interface Client : NSManagedObject
@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * middleName;
@property (nonatomic, retain) NSString * lastName;
@property (nonatomic, retain) Styles *clientStyles;
@end
样式是Client下面的嵌套对象。这是一对一的关系。当它从JSON中的服务器下来时,它看起来像这样。
{
"firstName": "",
"middleName": "",
"lastName": "",
"firstStyle": {
"styleId": 4,
"name": "",
"description": "",
"stylingTime": "55 min",
"stylingProductUsage": "A lot",
"chemicals": "LOTS O'GEL",
"deleted": false,
"modifiedOn": 1357161168830
}
}
一切都在一个不错的单一对象中。我可以把它拉下来并将它映射到我的对象没问题。当我需要将其返回给服务器时,会出现此问题。它需要采用这种格式。
{
"firstName": "",
"middleName": "",
"lastName": "",
"styles": [
{
"styleId": 4,
"name": "",
"description": "",
"stylingTime": "55 min",
"stylingProductUsage": "A lot",
"chemicals": "LOTS O'GEL",
"deleted": false,
"modifiedOn": 1357161168830
}]
}
这是非常有问题的,因为返回映射将样式实体放在数组中,而不是一对一。到目前为止,我已将此作为我的RKRequestDescriptor
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{
@"firstName": @"firstName",
@"middleName": @"middleName",
@"lastName": @"lastName",
}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[Client class]
rootKeyPath:nil];
HECK如何创建映射,以便返回一个Style对象数组,其中包含一个值???
答案 0 :(得分:0)
狂野猜测,但映射非常聪明,你不能做类似的事情:
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{
@"firstName": @"firstName",
@"middleName": @"middleName",
@"lastName": @"lastName",
}];
RKObjectMapping *stylesMappingDescription = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{
@"properties": @"here"
}];
[requestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"styles.0" toKeyPath:@"styles" withMapping:stylesMappingDescription]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[Client class]
rootKeyPath:nil];
(请注意styles.0为fromKeyPath
)