RestKit JSON空值崩溃 [英] RestKit JSON null value crash

查看:95
本文介绍了RestKit JSON空值崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题.我正在Swift中开发应用程序,并使用RestKit检索数据并将其发布回API.但是我遇到了路障.如果检索到的JSON有效负载包含一些空值,则该应用程序将崩溃并显示以下消息:

I have this problem. I am developing app in Swift and using RestKit to retrieve and post data back to API. However I have hit road block. If retrieved JSON payload contains some null value, the app wil crash with message:

***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[NSNull长度]:无法识别的选择器已发送到实例0x1994faba0'

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x1994faba0'

我该怎么办? 映射的属性是字符串.

What do I do? The mapped properties are Strings.

映射的对象:

public class User: NSObject {

    public var id: Int? = 0
    public var email: String?
    public var firstName: String?
    public var lastName: String?
    public var name: String?
    public var office: String?
    public var phone: String?
    public var company: String?

}

映射:

let userMapping = RKObjectMapping(forClass: User.self)
userMapping.addAttributeMappingsFromDictionary([
    "id": "id",
    "email": "email",
    "first_name": "firstName",
    "last_name": "lastName",
    "name": "name",
    "company": "company",
    "phone": "phone",
    "office": "office",
])
let responseDescriptor = RKResponseDescriptor(mapping: userMapping, pathPattern: currentUserPath, keyPath: "data", statusCodes: NSIndexSet(index: 200))
objectManager.addResponseDescriptor(responseDescriptor)

JSON响应:

{
  "status": "ok",
  "data": {
    "id": 1,
    "email": "some@email.com",
    "created_at": 1418832714451,
    "updated_at": 1421077902126,
    "admin": true,
    "first_name": "John",
    "last_name": "Doe",
    "company": null,
    "office": null,
    "phone": null,
    "name": "John Doe"
  }
}

它在以下各项上崩溃:办公室,电话和姓名.

It crashes on each of these: office, phone and name.

推荐答案

@Hotlicks是正确的,null应该由客户端处理.我相信这次崩溃的原因是Restkit无法正确地检查Swift中的类属性类型.我们在项目中通过向RKObjectMapping添加显式targetClass来解决了这一问题.因此,不要使用addAttributeMappingsFromDictionary方法,请尝试:

@Hotlicks is right, nulls should be handled by the client. I believe the reason for this crash is that Restkit cannot properly introspect class property types in Swift. We solved this in our project by adding explicit targetClass to our RKObjectMapping. So, instead of using the addAttributeMappingsFromDictionary method, try:

let userMapping = RKObjectMapping(forClass: User.self)
let simpleMapping = RKAttributeMapping(fromKeyPath: "first_name", toKeyPath: "firstName")
simpleMapping.propertyValueClass = NSString.classForCoder() // we used class for coder when we did our project, but you might have success with something else.
userMapping.addPropertyMapping(simpleMapping)

对于关系,您也必须这样做,就像这样:

You have to do the same for relationships as well, like so:

let relationshipMapping = RKRelationshipMapping(fromKeyPath: "person", toKeyPath: "person", withMapping: Person.self)
relationshipMapping.propertyValueClass = Person.classForCoder()
userMapping.addPropertyMapping(relationshipMapping)

这允许从NSNull自动转换为Swift可选.

This allows an automatic conversion from NSNull to an Swift optional.

这篇关于RestKit JSON空值崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆