加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift编程NSErrorPointer错误等

发布时间:2020-12-14 06:07:46 所属栏目:百科 来源:网络整理
导读: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,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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读