如果 json 值 == NULL,则 RESTKIT 内部映射失败
我正在使用 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] 错误。
如何在客户端进行检查并解决此问题,而无需更改服务器端的实现?
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
RKObjectMapping* 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管您没有显示实际解码 JSON 的代码,但我假设您使用的是正确遵循
NSNull
类的第三方库。在这种情况下,您可以使用以下命令检查键 @"x" 的对象是否为 null: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: