閱讀this answer所以經過測繪串,我不看我的情況是怎樣的任何不同,但所提出的解決方案似乎並不在RestKit 0.20.3RestKit 0.20陣列
工作對於以下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
我想將photos
和imageUrl
映射到分別Picture
的photoId
和imageUrl
。我以爲我可以用我的Job
的映射如下:
RKEntityMapping *ret = [RKEntityMapping mappingForEntityForName:@"Job" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore ];
[ret addAttributeMappingsFromDictionary: @{
@"photos": @"photos.photoId",
@"imageUrl": @"photos.imageUrl",
}];
然而,映射Job
始終沒有在photos
財產結束。
我知道我正在做正確的所有中間步驟(比如實際使用ret
進行映射),因爲JSON是一個包含許多其他密鑰的片段,我可以在沒有任何問題的情況下進行映射。我究竟做錯了什麼?請告訴我
編輯
改變映射代碼按北斗七星的回答是:
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
爲photoId
和imageUrl
(是的,我檢查過以確保我的拼寫和大小寫正確的JSON鍵)
仍然無法捕獲值,請參閱上文。作爲附註,如果'photos'中有4個對象,則只創建1個'Picture' – StaRbUck42
您所做的更改與我所建議的不同 - 因爲它不起作用。 RestKit無法以這種方式映射多個數組。你有什麼可以做的照片編號,但沒有和圖像的網址。唯一明智的做法是更改JSON或將數組映射到Job作品實體中的數組屬性。 – Wain
是的,我最終將JSON改爲將'photoId'和'imageUrl'數組合併爲一個具有'photoId'和'imageUrl'的對象數組作爲屬性 – StaRbUck42