iosobjective-cmappingrestkit-0.20

RestKit : how to map an array with correct type?


Got:

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

And:

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

And I receive Json:

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

Unfortunately, list will be a mix of NSString and NSNumber. How to tell RestKit 0.2x I only want NSNumber or NSString in my NSArray?


Solution

  • Rather than rewriting a JSON parser to do your particular task, why not just post-process 'list' and check each object with if([list[i] isKindOfClass:[NSString class]]) and convert it to an NSNumber?