gpt4 book ai didi

ios - Swift - 从 'NSData?' 转换为无关类型 'NSDictionary' 总是失败

转载 作者:行者123 更新时间:2023-11-28 13:14:04 25 4
gpt4 key购买 nike

我使用的是最新版本的 Xcode,我收到了这个错误:“Cast from 'NSData?'到不相关的类型 'NSDictionary' 总是失败"

使用此代码:

let dataObject = NSData(contentsOfURL: location)

let weatherDictionary: NSDictionary = NSJSONSerialization.dataWithJSONObject(dataObject!, options: nil, error: nil) as! NSDictionary

编程语言:Swift

更新代码:

if (error == nil) {
let dataObject = NSData(contentsOfURL: location)

let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as! NSDictionary

}
  • 现在在我运行我的应用程序时获取断点。

抱歉,我对堆栈溢出还是个新手

最佳答案

当然失败了。您调用了错误的方法。

NSJSONSerialization 方法 dataWithJSONObject 接受一个可以转换为 JSON 的对象并返回 NSData。它总是返回 NSData(或 nil)。在任何情况下,它都不会返回字典。

如果您想将 NSData 转换为集合对象,那么您需要改用 JSONObjectWithData 方法。

关于ios - Swift - 从 'NSData?' 转换为无关类型 'NSDictionary' 总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29980799/

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