gpt4 book ai didi

ios - RestKit JSON 空值崩溃

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:50 25 4
gpt4 key购买 nike

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

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 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 代码库中有关于空值绑定(bind)的更改。

关于ios - RestKit JSON 空值崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27917104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com