开发者

RESTKIT internal mapping fails if json value == NULL

开发者 https://www.devze.com 2023-04-07 14:54 出处:网络
I am using RESTKIT to map the properties from the server side to the properties in the client side. I am hitting the [NSNull unsignedIntValue] error when RESTKIT is trying to map a NULL value from th

I am using RESTKIT to map the properties from the server side to the properties in the client side.

I am hitting the [NSNull unsignedIntValue] error when RESTKIT is trying to map a NULL value from the server to a NSUInteger property on the client side.

For example:

//User object property "new_questions_count" defined on client side with NSUInteger property
@interface User : NSObject <NSCoding>
{
    NSUInteger new_questions_count;
}

@property (nonatomic, assign) NSUInteger new_questions_count;

@end

//User Object Mapping - mapping "new_question_count" to server's json value
RKObjectMap开发者_开发技巧ping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapAttributes:@"new_question_count",nil];
[provider setMapping:userMapping forKeyPath:@"user"];

For the above scenario, I will hit the [NSNull unsignedIntValue] error if the json value is "new_questions_count":null.

How can I do a check on the client side and resolve this without having to change the implementation on the server side?


Although you have not showed the code where you actually decode the JSON, I am assuming that you are using a third party library that correctly respects the NSNull class. In this case, you can check if the object for key @"x" is null using this:

if ([[dictionary objectForKey:@"x"] isKindOfClass:[NSNull class]]) {
    // it is NULL
} else {
    // it is not NULL, but it still might
    // not be in the dictionary at all.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消