0
我有一個對象OrganizationalUnit它通過parentId屬性鏈接到父實體。 OrganizationalUnit還具有主鍵屬性或(從JSON「_id」字段映射)。RestKit映射對象參考ID
這是我的代碼:
RKManagedObjectMapping *ouMapping = [RKManagedObjectMapping mappingForClass:[OrganizationalUnit class] inManagedObjectStore:manager.objectStore];
[ouMapping mapAttributes:@"name",@"type", nil];
[ouMapping mapKeyPath:@"_id" toAttribute:@"ouId"];
[ouMapping setPrimaryKeyAttribute:@"ouId"];
[ouMapping mapKeyPath:@"parent" toAttribute:@"parentId"];
[ouMapping hasOne:@"parent" withMapping:ouMapping];
[ouMapping connectRelationship:@"parent" withObjectForPrimaryKeyAttribute:@"parentId"];
其中一期工程在RestKit中發現的例子同樣的方式:
RKManagedObjectMapping* taskMapping = [RKManagedObjectMapping mappingForClass:[Task class] inManagedObjectStore:objectStore];
taskMapping.primaryKeyAttribute = @"taskID";
[taskMapping mapKeyPath:@"id" toAttribute:@"taskID"];
[taskMapping mapKeyPath:@"name" toAttribute:@"name"];
[taskMapping mapKeyPath:@"assigned_user_id" toAttribute:@"assignedUserID"];
[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"];
// Hydrate the assignedUser association via primary key
[taskMapping hasOne:@"assignedUser" withMapping:userMapping];
[taskMapping connectRelationship:@"assignedUser" withObjectForPrimaryKeyAttribute:@"assignedUserID"];
,應在本地創建基於IDS對象之間的關係。
不幸的是,在運行時,當接收到的數據的實際映射正在發生的事情,我得到的錯誤:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x6d9c6f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key _id.'
任何想法?
你可以發佈你試圖映射的JSON嗎?我有類似的問題與RestKit,可能會在解決方案(請參閱http://stackoverflow.com/questions/13054112/mapping-relationships-in-restkit-through-an-array-of-ids-doesnt-work ) – gummihaf