1

我正在使用 RESTKIT 将服务器端的属性映射到客户端的属性。

当 RESTKIT 尝试将 NULL 值从服务器映射到客户端的 NSUInteger 属性时,我遇到了 [NSNull unsignedIntValue] 错误。

例如:

//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
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapAttributes:@"new_question_count",nil];
[provider setMapping:userMapping forKeyPath:@"user"];

对于上述情况,如果 json 值为 "new_questions_count":null,我将遇到 [NSNull unsignedIntValue] 错误。

如何在客户端进行检查并解决此问题,而无需更改服务器端的实现?

4

1 回答 1

0

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.
}
于 2011-09-25T14:18:01.677 回答