如何使用Restkit映射

时间:2012-11-12 07:01:52

标签: restkit

我正在使用开发分支,以避免在新的推出时从旧的开始。不幸的是,我无法弄清楚如何做基础知识(我是RestKit的新手)。

我尝试的基本步骤是完成以2个参数调用“/ auth / login /”并重新获取json文档。任何帮助将不胜感激!

更新1:我问的是错误的问题吗?我错过了什么?人们只是不使用Restkit进行项目吗?

更新2:收到此错误时应该注意什么?我有一个类映射和一个路径模式,但我真的不知道我应该做什么。

Code=1001 "Unable to find any mappings for the given content"

我刚刚在https://github.com/RestKit/RestKit/blob/development/README.md

找到了这个更新的自述文件

更新3:我尝试了多种方式来进行简单的呼叫/响应/成功呼叫,但没有。我可以在输出中看到调用成功,但RestKit总是抱怨它无法映射内容。我真的不明白。产生的json主要如下:

{
      "email" : "me@here.com",
      "fullname" : "Full Name"
}

无论我尝试什么,我都无法让RestKit弄明白。救命?任何人吗?

更新4:我将有效负载更改为以下内容,并且我更改了描述符语句,但结果没有变化。调用成功,RestKit因错误1001而失败。它仍然表示我的keyPath = null。我错过了什么?

{
    "whoami" : {
      "email" : "me@here.com",
      "fullname" : "Full Name"
    }
} 

[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:mymap
                                               pathPattern:@"/auth/login/"
                                                   keyPath:@"whoami"
                                               statusCodes:statusCodes]];

1 个答案:

答案 0 :(得分:1)

也许这可以帮助..请求可能如下所示:

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:email forKey:@"email"];
[params setObject:fullname forKey:@"fullname"];


NSMutableURLRequest *rq = [manager requestWithObject:[User new] method:RKRequestMethodPOST path:@"http://url" parameters:params];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[User mapping] pathPattern:nil keyPath:nil statusCodes:nil];

RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:rq responseDescriptors:@[responseDescriptor]];

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {

     //success

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

     //error

}];
[operation start];

和映射:

+ (RKObjectMapping *)mapping
{
    RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[self class]];
    [requestMapping addAttributeMappingsFromDictionary:@{
     @"ContactDetail":     @"contactDetail",
     @"IdUser":            @"idUser",
     @"Name":              @"name",
     @"Address":           @"address",
     @"UserSettings":      @"userSettings"
     }];

    // if the obj have relationship
    RKRelationshipMapping *rl = [RKRelationshipMapping relationshipMappingFromKeyPath:@"someKey" toKeyPath:@"toKey" withMapping:[Obj mapping]];
    [requestMapping addPropertyMappingsFromArray:@[rl]];

    return requestMapping;
}