0

我有JSON如下回应:

 {
   response : {
        data : {
             subjects : [
                {
                },
                {
                }
             ]
        }
   }
  }

我有一个 NSManagedObject 如下:

@interface Department : NSManagedObject

@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *subjects;

@end

我在Department表中有一个部门记录,id = 1,name = "Physics"。我想用subjects从响应 JSON 数据中获得的来修改这个现有的部门。仅要更新的主题字段应保持不变。你能告诉我这个 RestKit 0.20 映射代码吗?

更新

这是我执行的映射:

NSDictionary *subMappingDict = @{
    @"id": @"id",
    @"sub_name": @"name"
};

RKEntityMapping *subMapping = [RKEntityMapping mappingForEntityForName:@"Subject" inManagedObjectStore:managedObjectStore];
[subMapping addAttributeMappingsFromDictionary:subMappingDict];
subMapping.identificationAttributes = @[@"id"];

RKEntityMapping *deptMapping = [RKEntityMapping mappingForEntityForName:@"Department" inManagedObjectStore:managedObjectStore];
[deptMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"subjects" toKeyPath:@"subjects" withMapping:subMapping]];

RKEntityMapping *collegeMapping = [RKEntityMapping mappingForEntityForName:@"College" inManagedObjectStore:managedObjectStore];
[collegeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"data" toKeyPath:@"department" withMapping:deptMapping]];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:collegeMapping pathPattern:nil keyPath:@"response" statusCodes:statusCodes];

我有一个现有的College记录,其中有一个现有的Department记录。subjects在这里,我只想通过映射更新部门的字段。

4

0 回答 0