【问题标题】:RestKit responseDescriptor mapping without KVC没有 KVC 的 RestKit responseDescriptor 映射
【发布时间】:2014-02-14 11:38:58
【问题描述】:

我正在尝试从 API url (...com/api/login?params...) 映射此响应

{"success":0,"error":"Some error"}

但我无法让它工作。我有这门课:

@interface LoginResponse : NSObject

@property(nonatomic) NSNumber * Success;
@property(nonatomic) NSString * Error;

@end

这在我的 AppDelegate.m 中有(以及其他有效的响应描述符):

RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[LoginResponse class]];
[loginMapping addAttributeMappingsFromDictionary:@{
                                                   @"success" : @"Success",
                                                   @"error" : @"Error",
                                                  }];

RKResponseDescriptor *loginResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
                                                                                             method:RKRequestMethodAny
                                                                                        pathPattern:@"/login"
                                                                                            keyPath:nil
                                                                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[manager addResponseDescriptor:loginResponseDescriptor];

我这样称呼它:

RKObjectManager *manager = [RKObjectManager sharedManager];

[manager getObjectsAtPath:@"/api/login"
               parameters:@{@"nickname":@"...",@"password":@"test"}
                  success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
 {
     NSArray* statuses = [mappingResult array];

 }
                  failure:^(RKObjectRequestOperation *operation, NSError *error)
 {
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                     message:[error localizedDescription]
                                                    delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
     [alert show];
     NSLog(@"Hit error: %@", error);
 }];

我得到了这些错误:

Hit error: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x10a2b48d0 {NSLocalizedDescription=No mappable object representations were found at the key paths searched., NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: events, points, results
The representation inputted to the mapper was found to contain nested object representations at the following key paths: error, success
This likely indicates that you have misconfigured the key paths for your mappings., keyPath=null, DetailedErrors=(
)}

我就是无法让它工作。我正在尝试查看 RestKit 的 wiki,但我认为我还可以。对我的情况进行了一些更改,但我想它应该可以工作。我有什么错?谢谢

【问题讨论】:

    标签: ios ios7 restkit restkit-0.20


    【解决方案1】:

    您的问题是与您的基本 URL 相关的路径模式。它们目前不匹配。

    我会将 bast URL 设置为:...com/api/。那么响应描述符路径模式和获取对象路径都是login。现在一切都匹配了。

    如果基本 URL 设置为 ...com,则路径模式两者都需要为 /api/login

    【讨论】:

    • 我之前尝试过“api/login”,但没有成功。但是现在我尝试了“/api/login”并且它有效。谢谢
    • 我通常将基本 URL 设置得越长越好,并包含尾部斜杠,因此在任何地方的路径模式中都不需要它。
    • 所以您认为当我一直使用/api/ 并将其从pathPatternsgetObjectsAtPath 中删除时,最好将基本url 用作http://www...com/api/
    • 就个人而言,是的,但这完全是个人喜好。只要一切都匹配,它们在功能上是等效的。
    猜你喜欢
    • 1970-01-01
    • 2013-03-04
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多