我正在尝试在我的响应正文中序列化 SyncRewardDataRequestModel 。 "的 an_id "序列化很好。但是,NSArray中的 SyncRewardDataInputModel 对象始终序列化为空的NSArray。我已经确认我在NSArray中传递了正确的值 - 有人看到我的映射有什么不正确吗?
类:
@interface SyncRewardDataInputModel : NSObject
@property (nonatomic,copy) NSNumber *test_id;
@end
@interface SyncRewardDataRequestModel : NSObject
@property (nonatomic,strong) NSArray *syncRewardDataInputs;
@property (nonatomic,copy) NSNumber *an_id;
@end
以下是我的回复描述符:
//Populate mapping
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
[requestMapping addAttributeMappingsFromDictionary:@{@"an_id": @"an_id"}];
RKObjectMapping *syncRewardDataInputsMapping = [RKObjectMapping mappingForClass:[SyncRewardDataInputModel class]];
[syncRewardDataInputsMapping addAttributeMappingsFromDictionary:@{@"test_id": @"test_id"}];
//Combine
RKRelationshipMapping *arrayRelation = [RKRelationshipMapping relationshipMappingFromKeyPath:@"syncRewardDataInputs" toKeyPath:@"downloadCardResponseDTOs" withMapping:syncRewardDataInputsMapping];
[requestMapping addPropertyMapping:arrayRelation];
//Put it in a request
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[SyncRewardDataRequestModel class] rootKeyPath:nil method:RKRequestMethodAny];
return requestDescriptor;
答案 0 :(得分:4)
这一行:
RKObjectMapping *syncRewardDataInputsMapping = [RKObjectMapping mappingForClass:[SyncRewardDataInputModel class]];
应该是:
RKObjectMapping *syncRewardDataInputsMapping = [RKObjectMapping requestMapping];
因为对于请求,您总是尝试映射到NSMutableDictionary
,以便可以从中生成JSON以进行传输。
通常,您将拥有一个用于接收数据的映射,链接到响应描述符,您可以使用inverseMapping
来生成用于请求描述符的映射。