2

所以我正在尝试使用 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>";
}

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

对象存在但无法查看

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

4

3 回答 3

2

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

- (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];
}
于 2013-01-02T01:07:54.907 回答
1

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

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

于 2013-01-04T12:36:40.947 回答
-1
 RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping
                                                                                    pathPattern:nil
                                                                                    keyPath:nil
                                                                                    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
于 2013-01-03T03:16:51.163 回答