在Restkit中映射单个JSON对象时出错 [英] Error mapping single JSON object in Restkit

查看:80
本文介绍了在Restkit中映射单个JSON对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将JSON响应映射到Objective-C对象。

I'm having trouble mapping a JSON-response into an Objective-C object.

这是响应的内容:

{
"pin": {
"title": "Donkey Kong Tower",
"description": "This is an article with #suchhashtag because it's awesome!",
"_id": "538054c107bf5b0b00795b82",
"user": "5380cc64db86cc0b002cdc63",
"updatedAt": "2014-06-15T21:34:41.891Z",
"createdAt": "2014-06-15T21:34:41.891Z",
"coordinates": {
  "latitude": 48.1678645,
  "longitude": 11.5861475
},
"id": "538054c107bf5b0b00795b82"
}
}

相应的对象标题如下所示:

The corresponding object header looks like this:

@interface Pin : NSObject

@property (strong, nonatomic) NSString *identifier;
@property (strong, nonatomic) NSString *description;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *latitude;
@property (strong, nonatomic) NSString *longitude;
@property (strong, nonatomic) NSString *userId;
@property (strong, nonatomic) NSString *pictureId;
@property (strong, nonatomic) NSString *recordingId;

@end

最后是映射设置:

RKObjectMapping* pinMapping = [RKObjectMapping mappingForClass:[Pin class]];

[pinMapping addAttributeMappingsFromDictionary:@{@"coordinates.latitude": @"latitude",
                                            @"coordinates.longitude": @"longitude", 
                                            @"id": @"identifier", 
                                            @"user" : @"userId", 
                                            @"title" : @"title",
                                            @"pictureId": @"pictureId", 
                                            @"recordingId": @"recordingId", 
                                            @"description" : @"description"}];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://server.herokuapp.com/pins/%@?access_token=%@", identifier, accessToken]]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor     
                                        responseDescriptorWithMapping:pinMapping 
                                        method:RKRequestMethodAny 
                                        pathPattern:nil 
                                        keyPath:@"pin" 
                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request 
                                        responseDescriptors:@[responseDescriptor]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
  RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];

日志文件表明映射确实有效,但生成的对象只包含description字段的内容。

The logfile indicates that the mapping does work, but the resulting object only contains the content of the description field.

restkit.object_mapping:RKMapperOperation.m:229 Asked to map source object {
    "_id" = 538054c107bf5b0b00795b82;
    coordinates =     {
        latitude = "48.1678645";
        longitude = "11.5861475";
    };
    createdAt = "2014-06-15T21:36:08.412Z";
    description = "This is an article with #suchhashtag because it's awesome!";
    id = 538054c107bf5b0b00795b82;
    title = "Donkey Kong Tower";
    updatedAt = "2014-06-15T21:36:08.412Z";
    user = 5380cc64db86cc0b002cdc63;
} with mapping <RKObjectMapping:0x10b351d60 objectClass=Pin propertyMappings=(
    "<RKAttributeMapping: 0x10b35b170 _id => identifier>",
    "<RKAttributeMapping: 0x10b3555d0 coordinates.latitude => latitude>",
    "<RKAttributeMapping: 0x10b355020 pictureId => pictureId>",
    "<RKAttributeMapping: 0x10b355370 recordingId => recordingId>",
    "<RKAttributeMapping: 0x10b330cd0 title => title>",
    "<RKAttributeMapping: 0x10b356ee0 coordinates.longitude => longitude>",
    "<RKAttributeMapping: 0x10b357400 description => description>",
    "<RKAttributeMapping: 0x10b356d10 user => userId>"
)>
2014-06-15 23:36:08.454 Remarkable[7523:f03] D restkit.object_mapping:RKMappingOperation.m:859 Starting mapping operation...
2014-06-15 23:36:08.454 Remarkable[7523:f03] T restkit.object_mapping:RKMappingOperation.m:860 Performing mapping operation: <RKMappingOperation 0x10b3646f0> for 'Pin' object. Mapping values from object {
    "_id" = 538054c107bf5b0b00795b82;
    coordinates =     {
        latitude = "48.1678645";
        longitude = "11.5861475";
    };
    createdAt = "2014-06-15T21:36:08.412Z";
    description = "This is an article with #suchhashtag because it's awesome!";
    id = 538054c107bf5b0b00795b82;
    title = "Donkey Kong Tower";
    updatedAt = "2014-06-15T21:36:08.412Z";
    user = 5380cc64db86cc0b002cdc63;
} to object *nil description* with object mapping (null)
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath '_id'. Transforming from class '__NSCFString' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath '_id' to 'identifier'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath '_id' to 'identifier'. Value: 538054c107bf5b0b00795b82
restkit.object_mapping:RKMappingOperation.m:518 Did not find mappable attribute value keyPath 'pictureId'
restkit.object_mapping:RKMappingOperation.m:518 Did not find mappable attribute value keyPath 'recordingId'
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'title'. Transforming from class '__NSCFString' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'title' to 'title'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'title' to 'title'. Value: Donkey Kong Tower
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'description'. Transforming from class '__NSCFString' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'description' to 'description'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'description' to 'description'. Value: This is an article with #suchhashtag because it's awesome!
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'user'. Transforming from class '__NSCFString' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'user' to 'userId'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'user' to 'userId'. Value: 5380cc64db86cc0b002cdc63
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'coordinates.latitude'. Transforming from class '__NSCFNumber' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'coordinates.latitude' to 'latitude'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'coordinates.latitude' to 'latitude'. Value: 48.1678645
restkit.object_mapping:RKMappingOperation.m:438 Found transformable value at keyPath 'coordinates.longitude'. Transforming from class '__NSCFNumber' to 'NSString'
restkit.object_mapping:RKMappingOperation.m:453 Mapping attribute value keyPath 'coordinates.longitude' to 'longitude'
restkit.object_mapping:RKMappingOperation.m:469 Mapped attribute value from keyPath 'coordinates.longitude' to 'longitude'. Value: 11.5861475
restkit.object_mapping:RKMappingOperation.m:928 Finished mapping operation successfully...
restkit.object_mapping:RKMapperOperation.m:403 Finished performing object mapping. Results: {
    pin = "This is an article with #suchhashtag because it's awesome!";
    }

(我省略了时间戳)

我认为主要错误在于

(I've omitted the timestamps)
I think the main error lies within the

to object *nil description* with object mapping (null)

part。

另一个StackOverflow问题有类似的问题: RestKit 0.2 result是一个nil对象的数组,尽管虽然没有明确的解决方案,但映射似乎是成功的。

part.
Another StackOverflow question has a similar problem: RestKit 0.2 result is an array of nil objects, although mapping seems to be succesful, though without a clear solution.

推荐答案

因为 description 是用于返回实例描述的预定义方法 - 不要通过添加具有相同名称的属性来覆盖它。

Because description is a predefined method used to return a description of the instance - don't override it by adding your own property with the same name.

将您的房产名称更改为概览或类似内容,并更新映射目的地密钥。

Change the name of your property to 'overview' or something like that and update the mapping destination key.

这篇关于在Restkit中映射单个JSON对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆