4

我有一个名为 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

Styles 是 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 对象???

4

1 回答 1

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 as fromKeyPath

于 2013-04-12T09:00:40.423 回答