我无法将JSON响应映射到Objective-C对象。
这是回复的内容:
{
"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"
}
}
相应的对象标题如下所示:
@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字段的内容。
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!";
}
(我省略了时间戳)
我认为主要的错误在于
to object *nil description* with object mapping (null)
一部分。
另一个StackOverflow问题有一个类似的问题:RestKit 0.2 result is an array of nil objects, although mapping seems to be succesful,但没有明确的解决方案。
答案 0 :(得分:3)
因为description
是用于返回实例描述的预定义方法,所以不要通过添加自己的同名属性来覆盖它。
将您的媒体资源名称更改为&#39;概述&#39;或类似的东西,并更新映射目标密钥。