Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios RestKit映射问题。相同的关键路径、不同的路径模式&;请求方法_Ios_Objective C_Mapping_Restkit_Restkit 0.20 - Fatal编程技术网

Ios RestKit映射问题。相同的关键路径、不同的路径模式&;请求方法

Ios RestKit映射问题。相同的关键路径、不同的路径模式&;请求方法,ios,objective-c,mapping,restkit,restkit-0.20,Ios,Objective C,Mapping,Restkit,Restkit 0.20,我对RestKit有问题 我将responseDescriptors配置为使用generalResponseMapping将请求映射到/users/login,并使用userMapping将请求映射到/users/:userID 初始化代码: RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL]; ... RKObjectMapping *generalResponseMapping

我对RestKit有问题

我将
responseDescriptors
配置为使用
generalResponseMapping
将请求映射到
/users/login
,并使用
userMapping
将请求映射到
/users/:userID

初始化代码:

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];

    ...

RKObjectMapping *generalResponseMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[generalResponseMapping addAttributeMappingsFromArray:@[ @"status", @"token" ]];

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
userMapping.identificationAttributes = @[ @"userID" ];
[userMapping addAttributeMappingsFromArray:@[ @"avatar", @"name", @"email" ]];


void(^configureResponse)(RKMapping *,        RKRequestMethod,        NSString *,        NSString *,        NSIndexSet *) =
                       ^(RKMapping *mapping, RKRequestMethod method, NSString *pattern, NSString *keyPath, NSIndexSet *statusCodes)
{
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                            method:method
                                                                                       pathPattern:pattern
                                                                                           keyPath:keyPath
                                                                                       statusCodes:statusCodes];
    [objectManager addResponseDescriptor:responseDescriptor];
};

NSIndexSet *codes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

configureResponse(generalResponseMapping, RKRequestMethodPOST, @"users/login",   nil, codes);   
configureResponse(userMapping,            RKRequestMethodGET,  @"users/:userID", nil, codes);

    ...
但是当我尝试向
/users/login
发送POST请求时

- (void)loginWithUserdata:(NSDictionary *)userdata
{   
    NSString *path = @"users/login";

    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    [objectManager postObject:nil
                         path:path
                   parameters:userdata
                      success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
 {
    ...
 }
                      failure:^(RKObjectRequestOperation *operation, NSError *error)
 {
    ...
 }];
}
由于RestKit试图将我的
generalResponse
对象映射到
userMapping

restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
    status = 0;
    token = "some_fake_token";
} with mapping <RKEntityMapping:0x9bb9040 objectClass=User propertyMappings=(
    "<RKAttributeMapping: 0x9bc5980 avatar => avatar>",
    "<RKAttributeMapping: 0x9b64dc0 name => name>",
    "<RKAttributeMapping: 0x9bc5700 email => email>",
    "<RKAttributeMapping: 0x9bc6a20 id => userID>",
)>
如果我注释掉为
users/:userID
添加的responseDescriptor,我可以成功获得正确的映射,网络模块将记录:

restkit.network:RKObjectRequestOperation.m:248 POST 'http://api.myapp.com/users/login' (200 OK / 1 objects) [request=0.0286s mapping=7.6855s total=7.7190s]:
response.headers={
"Cache-Control" = "max-age=0, private, must-revalidate";
Connection = "keep-alive";
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 07 Oct 2013 23:18:06 GMT";
Etag = "\"555dbdf216a0f30229810af9411ad7c9\"";
"Keep-Alive" = "timeout=20";
Server = nginx;
Status = "200 OK";
"Transfer-Encoding" = Identity;
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-Request-Id" = "07803850-cfe3-4ccb-9a0d-46be980b0537";
"X-Runtime" = "0.004017";
"X-UA-Compatible" = "chrome=1";
"X-XSS-Protection" = "1; mode=block";
}
response.body={"status":0,"token":"some_fake_token"}

否则,我会在
-[RKMapperOperation mapSourceRepresentationWithMappingsDictionary]
中@autoreleasepool块的末尾获得EXC\u BAD\u访问权限,并且网络块日志记录保持安静。

不确定,因为请求类型应提供所需的拆分。这一行:

NSString *path = @"/users/login";
改为:

NSString *path = @"users/login";

在RestKit尝试匹配路径模式的一部分而不是另一部分(在响应描述符上)时,向路径模式的一部分添加斜杠可能会导致失败。

无效。根据你的建议更新了问题。你是被重定向还是什么?您是否调试了什么东西真正触动了网络并被接收回来(特别是标题)?感谢您的关注。更新了RestKit网络模块日志部分的问题。
RKResponseDescriptor
有许多“匹配…”方法,我将查看这些方法的调试情况,看看会发生什么。这两种模式应该都匹配,但是方法类型应该区分描述符。
NSString *path = @"users/login";