Reputation: 5160
So, I have this result from the service:
{
"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": ""
}
But for some reason I can't map CustomerPromotions
it into an array, this is how my 2 objects looks like:
This is 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
This is '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
This is the mapping:
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)]];
This is the POST request:
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);
}];
Upvotes: 0
Views: 56
Reputation: 119041
It just looks like your key name is wrong. You use a source key only, which means the source and destination should match but they don't actually match. Try:
RKRelationshipMapping *relationMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"CustomerPromotions" toKeyPath:@"customerPromotions" withMapping:customerPromotionsMapping];
[customerLoginMapping addPropertyMapping:relationMapping];
Upvotes: 1