【问题标题】:RestKit Warning - WARNING: Failed mapping nested object: (null)RestKit 警告 - 警告:映射嵌套对象失败:(null)
【发布时间】:2012-03-16 11:18:09
【问题描述】:

我刚开始学习 RestKit。我正在尝试使用它来使用 Foursquare api 获取附近的场地。但每次我尝试“objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects”时,我都会收到此警告“WARNING: Failed mapping nested object: (null)”。

我真的很困惑。以下是我在 init 方法中的映射代码:

 _objectManager = [RKObjectManager objectManagerWithBaseURL:@"https://api.foursquare.com"];

    RKObjectMapping *locationMapping = [RKObjectMapping mappingForClass:[Location class]];
    [locationMapping mapKeyPath:@"address" toAttribute:@"address"];
    [locationMapping mapKeyPath:@"city" toAttribute:@"city"];
    [locationMapping mapKeyPath:@"state" toAttribute:@"state"];
    [_objectManager.mappingProvider setMapping:locationMapping forKeyPath:@"location"];

    RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Groups class]];
    [venueMapping mapRelationship:@"location" withMapping:locationMapping];
    [_objectManager.mappingProvider setMapping:venueMapping forKeyPath:@"groups"];

    RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Response class]];
    [responseMapping mapRelationship:@"groups" withMapping:venueMapping];
    [_objectManager.mappingProvider setMapping:responseMapping forKeyPath:@"response"];

    return self;

以下是“locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation”中的代码

    NSString *resourceURLString = [NSString stringWithFormat:@"/v2/venues/search?ll=%f,%f&limit=10&client_id=LEA1MYN1Z1QWSBFIOA1T4FREVVNF0R1ADQKMA0AMGL3CN5N4&client_secret=M2W204MKEAO4KV5L4ZXUYWK2GC32PO0KT5PWYJQBP4FKBTMO",newLocation.coordinate.latitude, newLocation.coordinate.longitude];

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourceURLString delegate:self];
[_locationManager stopUpdatingLocation];

最后这段代码在“objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects”中

_objects = [objects retain];
NSLog(@"object that i receive is %@",objects);

以下是我在控制台中得到的响应:

2012-02-26 13:15:39.872 restKitPractice[8609:690b] W restkit.object_mapping:RKObjectMappingOperation.m:372 警告:映射嵌套对象失败:(null) 2012-02-26 13:15:39.873 restKitPractice[8609:207] 我收到的对象是( “响应:0x6b5cb20” )

您可以使用此链接从 Foursquare 查看 Venues json:

https://api.foursquare.com/v2/venues/search?ll=40.562362,-111.938689&limit=10&client_id=LEA1MYN1Z1QWSBFIOA1T4FREVVNF0R1ADQKMA0AMGL3CN5N4&client_secret=M2W204MKEAO4KV5L4ZXUYWK2GC32PO0KT5PWYJQBP4FKBTMO

任何帮助或建议将不胜感激,因为我真的在这个问题上苦苦挣扎。

谢谢 维克

【问题讨论】:

    标签: ios ios5 warnings restkit


    【解决方案1】:

    你应该看看object mapping docs。

    您的映射定义与您尝试解析的 json 不匹配。

    【讨论】:

      【解决方案2】:

      我意识到这是一个老问题,但我看到了同样的警告。原因是你有像 {"response":{}} 这样的 JSON,你想映射到一个对象。由于对象中没有键,它本质上映射到nil。因此,警告是无害的。您不会得到 Response 对象,因为服务器本质上返回了 RestKit 无法映射的内容,因为 JSON 中没有与您的任何属性映射匹配的键。 RestKit 通过警告让您知道这一点,并回退到不创建对象。

      【讨论】:

        猜你喜欢
        • 2015-11-07
        • 2013-08-01
        • 2014-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-16
        • 1970-01-01
        • 2014-02-25
        相关资源
        最近更新 更多