2014-06-15 59 views
0

嗨,我有以下格式的JSON:休息套件實體映射一個嵌套的數組

{ 
"entityList": [ 
    { 
     "type": "Author", 
    "code" : "XYZ" 
       "Books": [ 
       { 
         "id": 10, 
    "name": "book1" 
         "publisher": { 
           "code": "ABC" 
            } 
       } 
     ], 
   } 
] 
} 

這些都是entites的

@interface Author :NSManagedObject 

@property (nonatomic, retain) NSString * type; 
@property (nonatomic, retain) NSString * code; 
@property (nonatomic, retain) NSArray* Books; 

@end 

@interface Book :NSManagedObject 

@property (nonatomic, retain) NSNumber * id; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) Publisher* publisher; 

@end 


@interface Publisher :NSManagedObject 

@property (nonatomic, retain) NSString * code; 

@end 

這裏是映射

RKEntityMapping* Authormapping = [RKEntityMapping mappingForEntityForName:@"Author" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore]; 

    // setup the identification Attribute 
    Authormapping.identificationAttributes = @[@"code"]; 
    // set up the attribute mapping 

    [Authormapping addAttributeMappingsFromDictionary:@{@"code":@"code",@"Books":@"Books" 
}]; 

RKEntityMapping* Bookmapping = [RKEntityMapping mappingForEntityForName:@"Book" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore]; 
Bookmapping.identificationAttributes = @[@"id"]; 
    // set up the attribute mapping 

    [Bookmapping addAttributeMappingsFromDictionary:@{@"id":@"id" 
[l_mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"publisher" 
                       toKeyPath:@"publisher"          withMapping:[Publisher getMappingForRelationship]]]; 

這只是有圖書與字典陣列我無法弄清楚如何使NsArray包含圖書entites的對象。非常感謝你的幫助。

@wain:當我在覈心數據進行關係類成爲

@interface Author :NSManagedObject 

@property (nonatomic, retain) NSString * type; 
@property (nonatomic, retain) NSString * code; 
@property (nonatomic, retain) Book* Books; 

@end 

然後我用船的關係映射地圖冊。但它只會是一本書。我看不出我將如何將書籍設置爲NSSet */NSArray *,並將其聲明爲與核心數據中作者的關係。我不知道如何設置映射。如果不是太麻煩,請給我一個代碼示例。非常感謝你。

+0

爲什麼'NSArray'?在覈心數據中你應該使用關係,所以它應該是'NSSet'。然後使用'RKRelationshipMapping'。 – Wain

+0

因此,這應該是@property(nonatomic,retain)NSSet *書籍和映射將是[Authormapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@「Books」toKeyPath:@「Books」withMapping:[Book getMapping]];我對嗎? – user3678218

+0

當我這樣做,我得到NSManagedObjects的實體'作者'不支持-mutableSetValueForKey:屬性'書籍'。 BTW書是作者的一個屬性。 – user3678218

回答

0

很抱歉不能很快回復。 Wain的解決方案是正確的。我錯過鏈接的原因是因爲svn問題,在我的舊文件中仍顯示出來,所以所有核心數據生成的方法和NSSet都丟失了。對於那些剛接觸RestKit /核心數據並遇到此問題的人。如果你有一個要映射的核心數據實體的對象的嵌套數組,使得「一對多」的關係。並請確保您重新生成類。最好的辦法是刪除舊的。