RestKit 实体映射

Posted

技术标签:

【中文标题】RestKit 实体映射【英文标题】:RestKit Entity mapping 【发布时间】:2012-12-08 07:50:24 【问题描述】:

您好,有人可以教我如何进行嵌套的 Restkit 实体映射吗?我在调试时不断收到错误消息,下面是我的错误消息和代码

[__NSSetM insertObject:atIndex:]: 无法识别的选择器发送到实例 0x95269d0

Json 数据

Family =(
      
        id = "1";
        parentName = "Mr John";
        Child =(
                
                parentID = "1";
                childName = "James";
                age = "18";

            ,
            
                parentID = "1";
                childName = "ruby";
                age = "19";
                                ,
                            
                parentID = "1";
                childName = "ella";
                age = "20";
            
        );
    
);

我的 AppDelegate.m

 RKEntityMapping *familyMapping = [RKEntityMapping mappingForEntityForName:@"Family" inManagedObjectStore:managedObjectStore];
debtorMapping.identificationAttributes = @[ @"id" ];

[familyMapping addAttributeMappingsFromDictionary:@
 @"id": @"accNo",
 @"parentName": @"companyName"
 ];


RKEntityMapping *childMapping = [RKEntityMapping mappingForEntityForName:@"Child" inManagedObjectStore:managedObjectStore];

childMapping.identificationAttributes = @[ @"parentID"];

[childMapping addAttributeMappingsFromDictionary:@
 @"parentID": @"parentID",
 @"childName": @"childName",
 @"age": @"age"
 ];


 [familyMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Child" toKeyPath:@"Child" withMapping:childMapping]];


RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:familyMapping
                                                                                   pathPattern:nil
                                                                                       keyPath:@"Family"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[objectManager addResponseDescriptor:responseDescriptor];

家庭 NSManagedObject

@class Child;
@interface Family : NSManagedObject
@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSString * parentName;
@property (nonatomic, retain) NSSet *child;
@end
@interface Family (CoreDataGeneratedAccessors)
- (void)addChildObject:(Child *)value;
- (void)removeChildObject:(Child *)value;
- (void)addChild:(NSSet *)values;
- (void)removeChild:(NSSet *)values;
@end

子 NSManageObject

@class Family;
@interface Child : NSManagedObject
@property (nonatomic, retain) NSString * parentID;
@property (nonatomic, retain) NSString * childName;
@property (nonatomic, retain) NSString * age;
@property (nonatomic, retain) Family *family;
@end

【问题讨论】:

【参考方案1】:

我已经发现了我的错误。 [familyMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Child" toKeyPath:@"child" withMapping:childMapping]]; toKeyPath 字符串应该用小写字母分配。 “关注核心数据关系字符串”。

【讨论】:

以上是关于RestKit 实体映射的主要内容,如果未能解决你的问题,请参考以下文章

特定实体的 RestKit 映射

RestKit 对象映射 - 一个实体,两个映射

RestKit - 到同一实体的关系映射创建无限循环

Restkit/Core Data 关系映射,实体到相同类型的实体(父/子)

如何在restkit映射后和保存到实体之前清除旧实体?

RestKit 2.0:映射-非唯一时的第二个实体为零