gpt4 book ai didi

ios - 如何在 Swift 上使用 RESTKIT 处理来自 JSON 的 Null 映射

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:57 24 4
gpt4 key购买 nike

当从我的服务器收到包含空值(在 JSON 响应中)的答案时,RestKit 内部映射在处理空值时崩溃。

有什么办法可以克服这个问题吗?

我曾尝试扩展 NSNull 以包含“缺失的”方法,但这没有用,而且似乎是错误的。

以下是我的映射和目标代码以及 restkit 输出:

对象映射:

var tempmapping = RKObjectMapping(forClass: PRLAUser.classForCoder())
tempmapping.addAttributeMappingsFromDictionary([
"firstName" : "firstName",
"secondName" : "lastName",
"email" : "email",
"id" : "prlaToken"

])

目标代码 - 至少是被映射的变量:

var email : String?
var firstName : String?
var lastName : String?


var prlaToken : String?

崩溃日志:

Parola[6138:3119821] -[NSNull length]: unrecognized selector sent to instance 0x197b86ba0

2015-01-20 15:53:37.973 Parola[6138:3119821] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSNull 长度]:无法识别的选择器发送到实例 0x197b86ba0”* 首先抛出调用栈:(0x1867fa59c 0x196f040e4 0x186801664 0x1867fe418 0x186702b6c 0x100707964 0x100023dc8 0x187605b10 0x1000a34a0 0x1000a4078 0x1000a99d4 0x1000a8dfc 0x10009a8f4 0x1000995a8 0x10009bbb4 0x10009c294 0x10009ca84 0x1875f461c 0x1000e4754 0x1000e2acc 0x1875f461c 0x1876b626c 0x1007b4df0 0x1007bf854 0x1007b8120 0x1007c175c 0x1007c2f18 0x1977252e4 0x197724fa8)libc++abi.dylib:以 NSException 类型的未捕获异常终止

非常感谢有关此事的任何帮助。

最佳答案

版本 0.23.1 适合您。我在 RestKit github 页面上发布了问题并进行了临时修复。

https://github.com/RestKit/RestKit/issues/2155

在一个将 NSNull 转化为 String 的对象中,我做了这样的事情

public override func validateValue(ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKeyPath inKeyPath: String, error outError: NSErrorPointer) -> Bool {
if let memory = ioValue.memory {
if inKeyPath == "hint" && ioValue.memory is NSNull {
ioValue.memory = nil
return true
}
}
return super.validateValue(ioValue, forKeyPath: inKeyPath, error: outError)
}

你也可以改变 RKMappingOperationDataSource 在请求时返回委托(delegate) NO

- (BOOL)mappingOperationShouldSetUnchangedValues:(RKMappingOperation *)mappingOperation;

顺便说一句。 Stack 上已经存在此问题:RestKit JSON null value crash

关于ios - 如何在 Swift 上使用 RESTKIT 处理来自 JSON 的 Null 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28047237/

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