从这里抛出的错误不会被处理 [英] Errors thrown from here are not handled

查看:83
本文介绍了从这里抛出的错误不会被处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试解析iOS应用上的JSON时出现此问题:

I've got this problem trying to parse a JSON on my iOS app:

相关代码:

let jsonData:NSDictionary = try JSONSerialization.jsonObject(with: urlData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers ) as! NSDictionary

/* XCode error ^^^ Errors thrown from here are not handled */

有人能帮帮我吗?

推荐答案

中可能出现的错误让jsonData =尝试JSONSerialization。 .. 未得到处理。

您可以忽略可能的错误,并在发生错误时崩溃作为惩罚:

You can ignore a possible error, and crash as penalty if an error occurs:

let jsonData = try! JSONSerialization ...

或返回可选 ,所以 jsonData 在错误的情况下是 nil

or return an Optional, so jsonData is nil in error case:

let jsonData = try? JSONSerialization ...

或者您可以捕获并处理抛出的错误:

or you can catch and handle the thrown error:

do {
    let jsonData = try JSONSerialization ...
    //all fine with jsonData here
} catch {
    //handle error
    print(error)
}

你可能想学习 The Swift(3)Language

这篇关于从这里抛出的错误不会被处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆