Restkit帖子与嵌套对象

时间:2013-10-17 19:06:22

标签: ios restkit restkit-0.20

您好我是使用restkit的新手,我对如何发布嵌套对象感到困惑。我想发布以下json:

{
    "user": {
        "id": "77777-795C-4374-9A29-5C12FF8031F7",
        "firstname:": "joe",
        "lastname": "bob"
    },
    "device": {
        "token": "phPS2sxbRRvoBbdE8e1VT8nFIJ4R8aJ6rdoCJ8iP1luWm3gUJDMnRHBs1SarfosTsIeYMMxwraLCY35B4whaFIISdosHzqa96vkZANHFy5rusHcrnUQuFgJHnsnqOsgxgOPd8QbrZAAxIlmqCt0an5pH69PWZAXcOVuzFilo310oeNW04qWZCWb8GWYMkODcMuUZANCAAFfvZB4ZAyokBADz"
    }
}

我正在使用Restkit版本0.20.3

这就是我的映射:

RKObjectMapping *userMapping = [RKObjectMapping requestMapping];

[userMapping addAttributeMappingsFromDictionary:@{
                                                      @"id": @"id",
                                                      @"firstname": @"firstname",
                                                      @"lastname": @"lastname"
                                                      }];

RKObjectMapping *deviceMapping = [RKObjectMapping requestMapping];
[facebookMapping addAttributeMappingsFromDictionary:@{
                                                     @"token": @"token"
                                                     }];

RKObjectMapping *authMapping = [RKObjectMapping requestMapping];

[authMapping addPropertyMapping:[RKRelationshipMapping
 relationshipMappingFromKeyPath:@"user"
                      toKeyPath:@"user"
                    withMapping:userMapping]];

[authMapping addPropertyMapping:[RKRelationshipMapping
 relationshipMappingFromKeyPath:@"device"
                      toKeyPath:@"device"
                    withMapping:deviceMapping]];

RKRequestDescriptor *authDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:authMapping objectClass:[AuthCredentials class] rootKeyPath:nil method:RKRequestMethodPOST];
[manager addRequestDescriptor:authDescriptor];

User *user = [[User alloc] init];
user.id = @"77777-795C-4374-9A29-5C12FF8031F7";
user.firstname = @"joe";
user.lastname = @"bob";

Device *device = [[Device alloc] init];
device.token = @"phPS2sxbRRvoBbdE8e1VT8nFIJ4R8aJ6rdoCJ8iP1luWm3gUJDMnRHBs1SarfosTsIeYMMxwraLCY35B4whaFIISdosHzqa96vkZANHFy5rusHcrnUQuFgJHnsnqOsgxgOPd8QbrZAAxIlmqCt0an5pH69PWZAXcOVuzFilo310oeNW04qWZCWb8GWYMkODcMuUZANCAAFfvZB4ZAyokBADz";

AuthCredentials *cred = [[AuthCredentials alloc] init];
cred.user = user;
cred.device = device;

[manager postObject:cred path:@"/testpost" parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
 {

 }
failure:^(RKObjectRequestOperation *operation, NSError *error)
 {

}];

我的错误消息如下: Error Domain = org.restkit.RestKit.ErrorDomain Code = 1001“在搜索的关键路径中找不到可映射的对象表示。” UserInfo = 0x9427070 {DetailedErrors =( ),NSLocalizedFailureReason =映射操作无法在搜索的关键路径中找到任何嵌套对象表示: 发现输入到映射器的表示包含以下关键路径的嵌套对象表示:错误 这可能表示您错误配置了映射的关键路径。,NSLocalizedDescription =在搜索的关键路径中找不到可映射的对象表示。,keyPath = null}

0 个答案:

没有答案