2014-01-20 30 views
0

所以,我有這樣的結果從服務:不能在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

它只是看起來像你的鍵名是錯誤的。您只使用源密鑰,這意味着源和目標應匹配,但它們實際上不匹配。嘗試:

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

感謝男人,作品像魅力。 –