所以在阅读了这个答案之后,我看不出我的情况有什么不同,但是建议的解决方案似乎在 RestKit 0.20.3 中不起作用
对于以下 json:
{
"photos": [
"872ac577-3f31-47a0-966e-6f2ed2fbabcd"
],
"imageUrl": [
"http://domain/WebAPI/FileUpload/10232013629_21382571406.439790.jpeg"
]
}
映射到以下对象:
@class Picture, Site, Survey;
@interface Job : NSManagedObject
@property (nonatomic, retain) NSString * jobId;
@property (nonatomic, retain) NSString * jobName;
@property (nonatomic, retain) NSString * jobStatusId;
@property (nonatomic, retain) NSString * jobStatus;
@property (nonatomic, retain) NSString * projectId;
@property (nonatomic, retain) NSString * projectType;
@property (nonatomic, retain) NSString * siteId;
@property (nonatomic, retain) NSSet *surveys;
@property (nonatomic, retain) NSSet *photos;
@property (nonatomic, retain) NSDate *createdDate;
@property (nonatomic, retain) Site *site;
@end
看起来像这样photos
的对象在哪里:Picture
@interface Picture : NSManagedObject
@property (nonatomic, retain) NSData * data;
@property (nonatomic, retain) NSString * photoId;
@property (nonatomic, retain) NSString * pictureLoc;
@property (nonatomic, retain) NSString * imageUrl;
@property (nonatomic, retain) Job *job;
@property (nonatomic, retain) QuestionAnswer *question;
@end
我想将 and 分别映射photos
到imageUrl
' Picture
sphotoId
和imageUrl
中。我想我可以在我Job
的映射中使用以下内容:
RKEntityMapping *ret = [RKEntityMapping mappingForEntityForName:@"Job" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore ];
[ret addAttributeMappingsFromDictionary: @{
@"photos": @"photos.photoId",
@"imageUrl": @"photos.imageUrl",
}];
但是,映射的Job
结果总是在photos
属性中没有任何内容。
我知道我正确地执行了所有中间步骤(就像实际ret
用于映射一样),因为 JSON 是一个包含许多其他键的片段,我可以毫无问题地映射。我究竟做错了什么?请告诉我
编辑
根据 Wain 的回答更改了映射代码:
RKEntityMapping *ret = [RKEntityMapping mappingForEntityForName:@"Job" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore ];
RKEntityMapping *subMapping = [RKEntityMapping mappingForEntityForName:@"Picture" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore ];
[subMapping addAttributeMappingsFromDictionary: @{
@"photos":@"photoId",
@"imageUrl":@"imageUrl"
}];
[ret addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:nil
toKeyPath:@"photos"
withMapping:subMapping]];
现在我Picture
添加了一个对象,但是使用了nil
forphotoId
和imageUrl
(是的,我检查以确保 JSON 键的拼写和大写正确)