Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios RestKit JSON空值崩溃_Ios_Json_Swift_Restkit - Fatal编程技术网

Ios RestKit JSON空值崩溃

Ios RestKit JSON空值崩溃,ios,json,swift,restkit,Ios,Json,Swift,Restkit,我有这个问题。我正在Swift中开发应用程序,并使用RestKit检索数据并将其发布回API。然而,我遇到了路障。如果检索到的JSON负载包含一些空值,则应用程序将崩溃,并显示以下消息: ***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSNull length]:未识别的选择器已发送到实例0x1994faba0” 我该怎么办? 映射的属性是字符串 映射对象: public class User: NSObject { publ

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

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

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

映射对象:

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"
  }
}

办公室、电话和姓名都会崩溃。

尝试使用其他版本的RestKit。RestKit代码库中有关于空值绑定的更改。

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

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)
对于关系,您也必须这样做,例如:

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

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

显示JSON和映射。改进JSON。将NSNull的处理添加到您的代码中。我已编辑了我的问题以满足您的要求:)我如何使用RestKit处理NSNull?您使用的是什么版本(实际上应该转换为零)RestKit 0.24.0、Xcode 6.1.1、iOS 8.1如果我从映射中删除公司、办公室和电话,我确实尝试过旧版本,它将JSON中的null映射为Swift/Obj中的nil-C@ZdeněkTopič你能说出哪个版本吗?