【问题标题】:Rails 3.2.8 + RestKit - Mapping without KVCRails 3.2.8 + RestKit - 没有 KVC 的映射
【发布时间】:2012-12-30 02:23:04
【问题描述】:

所以我正在尝试使用 RestKit 从我的 Rails 应用程序中获取 JSON 格式的信息

我这样做的代码是这样的:

应用代理

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //Initialise RestKit
    NSURL *URL = [NSURL URLWithString:@"https://myapp.dev"];
    AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:URL];

    //Enable Activity Indicator Spinner
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;


    [client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];

    RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];


    RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Info class]];

    [infoMapping addAttributeMappingsFromDictionary:@{
     @"sample":@"sample",
     @"sample_1":@"sample_1"
     }];

    RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping
                                                                                        pathPattern:@"/info"
                                                                                        keyPath:nil
                                                                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [objectManager addResponseDescriptor:infoDescriptor];
}

查看文件

- (void)loadInfo
{

    RKObjectManager *objectManager = [RKObjectManager sharedManager];

    [objectManager getObjectsAtPath:@"/info"
                         parameters:nil
                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                                NSArray *info = [mappingResult array];

                                NSLog(@"Loaded info: %@", info);


                                _info = info;


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

                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting into"
                                                                                message:[error localizedDescription]
                                                                                delegate:nil
                                                                                cancelButtonTitle:@"OK"
                                                                                otherButtonTitles:nil];
                                [alert show];
                                NSLog(@"Hit error: %@", error);

                            }];
}

问题是。在日志输出中,RestKit 告诉我它成功映射了所有内容。但是当我尝试使用登录视图文件的方法和使用po 的调试器查看对象时,我得到以下信息

374 Finished performing object mapping. Results: {
    "<null>" = "<Info: 0xa291b30>";
}

我无法查看该对象,并且带有断点,它显示为:

我已经为此苦苦挣扎了几天,但我不确定还有什么可以尝试的。任何帮助将不胜感激

【问题讨论】:

    标签: ios restkit xcode4.5


    【解决方案1】:

    我遇到了类似的问题。我不太清楚为什么,但是当我执行以下操作时,它修复了它。

    - (void)loadInfo
    {
    
        RKObjectManager *objectManager = [RKObjectManager sharedManager];
    
        [objectManager getObjectsAtPath:@"/info"
                             parameters:nil
                                success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    
                                    NSArray *info = [mappingResult array]; 
                                    NSLog(@"Loaded info: %@", info);                                  
                                    _info = info;
    
    
                                } failure:^(RKObjectRequestOperation *operation, NSError *error) {
    
                                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting into"
                                                                                    message:[error localizedDescription]
                                                                                   delegate:nil
                                                                          cancelButtonTitle:@"OK"
                                                                          otherButtonTitles:nil];
                                    [alert show];
                                    NSLog(@"Hit error: %@", error);
    
                                }];
    }
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
        //Initialise RestKit
        NSURL *URL = [NSURL URLWithString:@"https://myapp.dev"];
        AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:URL];
    
        //Enable Activity Indicator Spinner
        [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    
    
        [client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];
    
        RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
    
    
        RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Info class]];
    
        [infoMapping addAttributeMappingsFromDictionary:@{
         @"sample":@"sample",
         @"sample_1":@"sample_1"
         }];
    
        RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping
                                                                                            pathPattern:@"/info/:id"
                                                                                            keyPath:nil
                                                                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    
        [objectManager addResponseDescriptor:infoDescriptor];
    }
    

    【讨论】:

    • 这适用于单个对象。但是说你的rest api(在这种情况下来自Rail)返回多个对象。如何将它们全部映射到一个对象中?
    • 你的 response.body 怎么样?
    • 它是 (null) 即使它有内容
    • 你有没有使用http scoop 或者wireshark 来嗅探响应包,看看内容是什么样子的?可以发一下内容吗?
    • gist.github.com/4462594 由于敏感,我删除了一些信息。但我正在尝试将一个对象与每个 JSON 条目映射为其中的一部分,并通过它们的 ID 组织它们
    【解决方案2】:

    当您加载没有嵌套 keyPath 的对象表示时,RestKit 将映射的对象存储在字典内的 [NSNull null] 键下(因为 nil 不是有效的字典键)。您可以通过在RKMappingResult 对象上调用firstObjectarraydictionary 来检索映射结果以访问映射的对象。

    我看到一个关于将数组映射到单个对象的后续问题...您的 JSON 是什么样的以及您试图如何表示它?

    【讨论】:

    • gist.github.com/4462594 由于敏感,我删除了一些信息。但我正在尝试将一个对象与每个 JSON 条目映射为其中的一部分,并通过它们的 ID 组织它们
    【解决方案3】:
     RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping
                                                                                        pathPattern:nil
                                                                                        keyPath:nil
                                                                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    

    【讨论】:

    • 你可以解释你的答案来帮助别人。谢谢。
    猜你喜欢
    • 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
    相关资源
    最近更新 更多