0

我正在尝试映射以下对象:

{"walletAccepted":false,"creditCardTypesAccepted":["visa","mastercard","discover","americanexpress"],"paypalAccepted":false}

到具有相同属性的对象。但是,ResourcePath 是动态的,因为它遵循 /paymentmethods/zone/:internalZoneCode

我在映射时所做的每一次尝试总是以“映射期间遇到错误:找不到 keyPath 的对象映射:''”而告终

我认为问题在于没有根密钥/密钥路径。我还尝试了以下方法,但没有运气:

RKURL *rkUrl = [RKURL URLWithBaseURL:[RKClient sharedClient].baseURL resourcePath:[NSString stringWithFormat:@"/paymentoptions/zone/%@", zoneNumber]];
RKObjectLoader* loader = [[RKObjectLoader alloc] initWithURL:rkUrl mappingProvider:[RKObjectManager sharedManager].mappingProvider];
loader.method = RKRequestMethodGET;
loader.delegate = self;

loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[PMZonePaymentMethods class]];
[loader send];
4

1 回答 1

0

我发现实现它的方法是配置映射,并将映射添加到对象管理器而不需要关键路径或资源k

所以像这样:

RKObjectMapping* zonePaymentMethodsMapping = [RKObjectMapping mappingForClass:[PMZonePaymentMethods class]];
[zonePaymentMethodsMapping mapKeyPath:@"walletAccepted" toAttribute:@"walletAvailable"];
[zonePaymentMethodsMapping mapKeyPath:@"creditCardTypesAccepted" toAttribute:@"cards"];
[zonePaymentMethodsMapping mapKeyPath:@"paypalAccepted" toAttribute:@"paypalAvailable"];

[[RKObjectManager sharedManager].mappingProvider addObjectMapping:zonePaymentMethodsMapping];

并使用它来提出请求:

RKURL *rkUrl = [RKURL URLWithBaseURL:[RKClient sharedClient].baseURL resourcePath:[NSString stringWithFormat:@"/paymentoptions/zone/%@", zoneNumber]];
RKObjectLoader* loader = [[RKObjectLoader alloc] initWithURL:rkUrl mappingProvider:      [RKObjectManager sharedManager].mappingProvider];
loader.method = RKRequestMethodGET;
loader.delegate = self;

loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider  objectMappingForClass:[PMZonePaymentMethods class]];
[loader send];
于 2012-07-26T14:14:11.233 回答