RestKit:如何映射具有正确类型的数组?

时间:2014-01-02 15:39:50

标签: ios objective-c mapping restkit-0.20

GOT:

@property(nonatomic, retain) NSString *user;
@property(nonatomic, retain) NSString *token;
@property(nonatomic, retain) NSArray *list;// NSNumber only please!

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[MyResponse class]];
[mapping addAttributeMappingsFromDictionary:@{
    @"user" : @"user",
    @"token" : @"token",
    @"list" : @"list",
 }];

我收到Json:

{"user":"foobar","token":"azerty","list":[0,1,"2"]}

不幸的是,list将是NSString和NSNumber的混合体。如何告诉RestKit 0.2x我只想在我的NSArray中使用NSNumber或NSString?

2 个答案:

答案 0 :(得分:2)

RestKit肯定会使用KVC,如果您只想要数字,则可以使用相应的keyPath,例如如果我希望数字始终是整数,我可以使用integerValueNSNumber都有的方法NSString

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[MyResponse class]];
[mapping addAttributeMappingsFromDictionary:@{
  @"user"              : @"user",
  @"token"             : @"token",
  @"list.integerValue" : @"list",
}];

答案 1 :(得分:1)

为什么不只是重写一个JSON解析器来完成你的特定任务,为什么不只是后处理'list'并用if([list[i] isKindOfClass:[NSString class]])检查每个对象并将其转换为NSNumber?