无法在restkit中将关系映射到数组中

时间:2014-01-20 12:53:46

标签: ios objective-c mapping restkit

所以,我从服务中获得了这个结果:

{
  "CustomerPromotions": [
    {
      "BottomTitle": "Thank you",
      "IconURL": "",
      "MiddleText": "399$",
      "PromotionID": "123B456",
      "SortOrder": 0,
      "TopTitle": "Welcome to"
    }
  ],
  "CustomerStatus": 1,
  "CustomerVoucherIcon": "",
  "CustomerVoucherText": "",
  "CustomerVoucherTitle": "",
  "ErrorID": 0,
  "ErrorMessage": "",
  "FeedbackIndicator": false,
  "FeedbackLowerText": "",
  "FeedbackTitle": "",
  "HPTopTitle": "Hello world",
  "LocalCurrencySign": "$",
  "LocalCurrencyValue": "3.30",
  "LocalTime": "20:30",
  "LocalWeather": "-5",
  "PopUpTopTitle": ""
}

但出于某种原因,我无法将CustomerPromotions映射到数组中,这就是我的2个对象的样子:

这是CustomerLogin.h

@interface CustomerLogin : NSObject
@property (nonatomic) NSArray *customerPromotions;
@property (strong, nonatomic) NSNumber *customerStatus;
@property (strong, nonatomic) NSString *customerVoucherIcon;
@property (strong, nonatomic) NSString *customerVoucherText;
@property (strong, nonatomic) NSString *customerVoucherTitle;
@property (strong, nonatomic) NSNumber *errorID;
@property (strong, nonatomic) NSString *errorMessage;
@property (strong, nonatomic) NSNumber *feedbackIndicator;
@property (strong, nonatomic) NSString *feedbackLowerText;
@property (strong, nonatomic) NSString *feedbackTitle;
@property (strong, nonatomic) NSString *hpTopTitle;
@property (strong, nonatomic) NSString *localCurrencySign;
@property (strong, nonatomic) NSString *localCurrencyValue;
@property (strong, nonatomic) NSString *localTime;
@property (strong, nonatomic) NSString *localWeather;
@property (strong, nonatomic) NSString *popUpTopTitle;
@end

这是' CustomerPromotions`:

@interface CustomerPromotions : NSObject
@property (strong, nonatomic) NSString *bottomTitle;
@property (strong, nonatomic) NSString *iconURL;
@property (strong, nonatomic) NSString *middleText;
@property (strong, nonatomic) NSString *promotionID;
@property (strong, nonatomic) NSNumber *sortOrder;
@property (strong, nonatomic) NSString *topTitle;
@end

这是映射:

    RKObjectMapping *customerLoginMapping = [RKObjectMapping mappingForClass:[CustomerLogin class]];
    [customerLoginMapping addAttributeMappingsFromDictionary:@{ @"CustomerStatus" : @"customerStatus",
                                                                @"CustomerVoucherIcon" : @"customerVoucherIcon",
                                                                @"CustomerVoucherText" : @"customerVoucherText",
                                                                @"CustomerVoucherTitle" : @"customerVoucherTitle",
                                                                @"ErrorID" : @"errorID",
                                                                @"ErrorMessage" : @"errorMessage",
                                                                @"FeedbackIndicator" : @"feedbackIndicator",
                                                                @"FeedbackLowerText" : @"feedbackLowerText",
                                                                @"FeedbackTitle" : @"feedbackTitle",
                                                                @"HPTopTitle" : @"hpTopTitle",
                                                                @"LocalCurrencySign" : @"localCurrencySign",
                                                                @"LocalCurrencyValue" : @"localCurrencyValue",
                                                                @"LocalTime" : @"localTime",
                                                                @"LocalWeather" : @"localWeather",
                                                                @"PopUpTopTitle" : @"popUpTopTitle" }];

    RKObjectMapping *customerPromotionsMapping = [RKObjectMapping mappingForClass:[CustomerPromotions class]];
[customerPromotionsMapping addAttributeMappingsFromDictionary:@{ @"BottomTitle" : @"bottomTitle",
                                                                 @"IconURL" : @"iconURL",
                                                                 @"MiddleText" : @"middleText",
                                                                 @"PromotionID" : @"promotionID",
                                                                 @"SortOrder" : @"sortOrder",
                                                                 @"TopTitle" : @"topTitle" }];

[customerLoginMapping addRelationshipMappingWithSourceKeyPath:@"customerPromotions" mapping:customerPromotionsMapping];

[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:customerLoginMapping
                                                                                                    method:RKRequestMethodPOST
                                                                                               pathPattern:@"CustomerLogin"
                                                                                                   keyPath:nil
                                                                                               statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

这是POST请求:

NSDictionary *params = @{ @"AppID" : @"1",
                          @"AppPassword" : @"String content",
                          @"Password" : @"password",
                          @"UserName" : @"username" };

[[RKObjectManager sharedManager] postObject:[[CustomerLogin alloc] init]
                                       path:@"CustomerLogin"
                                 parameters:params
                                    success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                        NSLog(@"%@", operation.HTTPRequestOperation.responseString);
                                        NSLog(@"%@", mappingResult.array);

                                        CustomerLogin *customer = [mappingResult.array lastObject];
                                        NSLog(@"%@", customer.customerPromotions);
                                        NSLog(@"%@", customer.hpTopTitle);

                                    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                        NSLog(@"%@", error.localizedDescription);
                                    }];

1 个答案:

答案 0 :(得分:1)

看起来你的核心名称错了。您仅使用源密钥,这意味着源和目标应匹配,但它们实际上不匹配。尝试:

RKRelationshipMapping *relationMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"CustomerPromotions" toKeyPath:@"customerPromotions" withMapping:customerPromotionsMapping];
[customerLoginMapping addPropertyMapping:relationMapping];