使用Restkit进行对象映射。将对象数组映射到NSSet

时间:2013-11-14 13:02:47

标签: ios json serialization restkit

我必须将数组映射到对象。我正在使用RKObjectMapping。一切都很好,但奖励数组没有被映射。获得奖励NSSet为null。

@interface VBDealDetail : NSObject

@property(nonatomic,retain) NSString *dealName;
@property(nonatomic,retain) NSNumber *dealID;
@property(nonatomic,retain) NSSet *rewards;

在我的控制器类......

RKObjectManager *manager                  = [[VBRestKit sharedDataManager] objectManager];    
RKObjectMapping *dealDetailEntryMapping   = [RKObjectMapping mappingForClass:[VBDealDetailEntries class]];    
RKObjectMapping *successResponseMapping   = [RKObjectMapping mappingForClass:[VBCommonResponse class]];
[successResponseMapping addAttributeMappingsFromDictionary:@{
 @"code":  @"code",
 @"msg" :  @"message"
 }];

RKObjectMapping* rewardMapping  = [RKObjectMapping mappingForClass:[VBRewards class] ];
// NOTE: When your source and destination key paths are symmetrical, you can use addAttributesFromArray: as a shortcut instead of addAttributesFromDictionary:
[rewardMapping addAttributeMappingsFromDictionary:@{@"rewardId":@"rewardID",
                                                    @"rewardTitle":@"rewardTitle" }];
RKObjectMapping *dealDetailMapping        = [RKObjectMapping mappingForClass:[VBDealDetail class]];

[dealDetailMapping addAttributeMappingsFromDictionary:@{

 @"dealId"                               : @"dealID",
 @"dealName"                             : @"dealName"
 }];
[dealDetailEntryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:  @"response"
                                                                                       toKeyPath:  @"commonResponse"
                                                                                     withMapping:  successResponseMapping]];

[dealDetailEntryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:  @"dealDetails"
                                                                                       toKeyPath:  @"dealDetail"
                                                                                     withMapping:  dealDetailMapping]];

[dealDetailEntryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"reward"
                                                                                       toKeyPath:@"rewards"
                                                                                     withMapping:rewardMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:   dealDetailEntryMapping
                                                                                   pathPattern:   nil
                                                                                       keyPath:   @"dealCollection"
                                                                                   statusCodes:   RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptor:responseDescriptor];

0 个答案:

没有答案