我有一个带有动态键的JSON对象,比如
carBrands:
{ 13950:{ brand_id:" 13950", 名称:"丰田", 国家:"日本", 零售商:"" }, 13633:{ brand_id:" 13633", 名称:" NISSAN", 国家:" JAPAN", 零售商:"" } }
需要使用普通的JSON对象进行映射。
汽车:
{ {id:" xyz" brand_id:" 13950", 状态:"待定", user_id:" user1" }, {id:" ABC", brand_id:“13633”, 状态:"已交付", user_id:" user2" } }
以下是carBrands对象的代码。
RKEntityMapping *carBrandsMapping = [RKEntityMapping mappingForEntityForName:@"CarBrands" inManagedObjectStore:managedObjectStore];
carBrandsMapping.forceCollectionMapping = YES;
[ carBrandsMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"brand_id"];
carBrandsMapping.identificationAttributes = @[@"brand_id"];
[ carBrandsMapping addAttributeMappingsFromDictionary:@{@"(brand_id).name": @"name",
@"(brand_id).retailer": @"retailer",
@"(brand_id).country":@"country"
}];
RKResponseDescriptor *brandsRD = [RKResponseDescriptor responseDescriptorWithMapping:carBrandsMapping method:RKRequestMethodGET pathPattern:nil keyPath:@”brands” statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
对于汽车对象,
RKEntityMapping *carsMapping = [RKEntityMapping mappingForEntityForName:@"Cars" inManagedObjectStore:managedObjectStore];
carsMapping.identificationAttributes = @[@"car_id"];
[carsMapping addAttributeMappingsFromDictionary:@{@"id": @"car_id",
@"brand_id": @"brand_id",
@"status": @"status",
@"user_id": @"user_id"
}];
RKResponseDescriptor *carsRD = [RKResponseDescriptor responseDescriptorWithMapping:carsMapping method:RKRequestMethodGET pathPattern:nil keyPath:@”cars” statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
我尝试了以下语句来定义关系。
[carsMapping addConnectionForRelationship:@"carBrands" connectedBy:@"brand_id"];
[carsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"carBrands" toKeyPath:@"carsBrands" withMapping:brandsMapping]];
我的问题是当我拿到“汽车”时。数据使用以下陈述,我的关系对象' carBrands'没有。我也找到了不可映射的内容'错误。如何解决?
[[RKObjectManager sharedManager] getObjectsAtPath:PATH_FOR_CAR parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
self.carsArray = mappingResult.array;
NSLog(@"%@",mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Load failed with error: %@", error);
}];
问题:
如何定义关系映射,以便获取' Cars'实体将拥有版权数据&carrandy'存储为故障而不是零?
@class CarBrands;
@interface Cars: NSManagedObject
@property (nonatomic, retain) NSString * car_id;
@property (nonatomic, retain) NSString * brand_id;
@property (nonatomic, retain) NSString * status;
@property (nonatomic, retain) NSString * user_id;
@property (nonatomic, retain) CarBrands *carBrands;
@end
谢谢。