swift编程NSErrorPointer错误等

swift编程NSErrorPointer错误等,第1张

概述var data: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: error) as NSDictionary; 这行代码给我错误 NSError is not convertable to NS
var data: NSDictionary =     NSJsONSerialization.JsONObjectWithData(responseData,options:NSJsONReadingOptions.AllowFragments,error: error) as NSDictionary;

这行代码给我错误

NSError is not convertable to NSErrorPointer.

所以我随后想到将代码更改为:

var data: NSDictionary =     NSJsONSerialization.JsONObjectWithData(responseData,error: &error) as NSDictionary;

这将把NSError错误转换为NSErrorPointer。但是,然后我得到一个新的错误,不能理解它:

NSError! is not a subtype of '@|value ST4'
您的NSError必须定义为可选,因为它可以是nil:
var error: NSError?

您还想要解释在解析中有错误,它将返回nil或解析返回一个数组。要做到这一点,我们可以使用一个可选的铸造与as?运算符。

这给我们留下了完整的代码:

var possibleData = NSJsONSerialization.JsONObjectWithData(    responseData,error: &error    ) as? NSDictionary;if let actualError = error {    println("An Error Occurred: \(actualError)")}else if let data = possibleData {   // do something with the returned data}
总结

以上是内存溢出为你收集整理的swift编程NSErrorPointer错误等全部内容,希望文章能够帮你解决swift编程NSErrorPointer错误等所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1059369.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存