我正在使用RestKit并且无法正确映射我的JSON响应中的图像链接。
响应:
{
"timestamp": "2013-05-10T03:09:39Z",
"feed": [{
"headline": "Head text",
"links": {
"api": {
"news": {
"href": "http://api.website.com/v1/91"
},
"self": {
"href": "http://api.website.com/v1/91"
}
},
"web": {
"href": "http://website.com/the/story"
},
"mobile": {
"href": "http://m.website.com/wireless/story?storyId=9254113"
}
},
"source": "Associated Press",
"description": "Description text.",
"images": [{
"height": 324,
"alt": "",
"width": 576,
"name": "Name text",
"caption": "Caption text.",
"url": "http://a.website.com/media/2013/0508.jpg"
}],
Feed.h
@property (nonatomic, strong) Links *links;
@property (nonatomic, strong) Images *images;
@property (nonatomic, strong) Video *video;
@property (nonatomic, strong) NSString *headline;
@property (nonatomic, strong) NSString *source;
@property (nonatomic, strong) NSDate *published;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSString *premium;
+ (RKObjectMapping *) mapping;
Feed.m
+ (RKObjectMapping *)mapping {
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
[mapping mapKeyPathsToAttributes:
@"headline", @"headline",
@"source", @"source",
@"published", @"published",
@"description", @"description",
@"premium", @"premium",
nil];
[mapping hasOne:@"links" withMapping:[Links mapping]];
[mapping hasOne:@"images" withMapping:[Images mapping]];
//[mapping hasMany:@"images" withMapping:[Images mapping]];
[mapping hasOne:@"video" withMapping:[Video mapping]];
}];
return objectMapping;
}
Images.h
@property (nonatomic, strong) NSNumber *height;
@property (nonatomic, strong) NSNumber *width;
@property (nonatomic, strong) NSString *caption;
@property (nonatomic, strong) NSURL *url;
+ (RKObjectMapping *) mapping;
Images.m
+ (RKObjectMapping *)mapping {
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
[mapping mapKeyPathsToAttributes:
@"height", @"height",
@"width", @"width",
@"caption", @"caption",
@"url", @"url",
nil];
}];
return objectMapping;
}
*错误:“W restkit.object_mapping:RKObjectMappingOperation.m:244 keyPath'images'的值转换失败。没有从'__NSArrayM'转换为'Images'的策略”*
其他一切正常,但图片无效,因为它们与所有其他数据略有不同。
真的无法弄清楚答案,会非常感谢任何帮助。
答案 0 :(得分:2)
更改images
Feed
的媒体资源
property (nonatomic, strong) NSArray *images;
并使用hasMany
关系
[mapping hasMany:@"images" withMapping:[Images mapping]];