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

swift try try? try!

发布时间:2020-12-14 05:11:40 所属栏目:百科 来源:网络整理
导读:try You have 2 options when you try calling a function that may throw. You can take responsibility of? handling errors ?by surrounding your call within a do-catch block: Or just try calling the function,and? pass the error along ?to the ne

try

You have 2 options when you try calling a function that may throw.

You can take responsibility of?handling errors?by surrounding your call within a do-catch block:

 

Or just try calling the function,and?pass the error along?to the next caller in the call chain:

 

try!

What happens when you try to access an implicitly unwrapped optional with a nil inside it? Yes,true,the app will CRASH! Same goes with try! it basically ignores the error chain,and declares a “do or die” situation. If the called function didn’t throw any errors,everything goes fine. But if it failed and threw an error,?your application will simply crash.

 

try?

A new keyword that was introduced in Xcode 7 beta 6. It?returns an optional?that unwraps successful values,and catches error by returning nil.

 

Or we can use new awesome guard keyword:

 

One final note here,by using?try??note that you’re discarding the error that took place,as it’s translated to a nil. Use try? when you’re focusing more on successes and failure,not on why things failed.

?

https://stackoverflow.com/questions/32390611/try-try-try-what-s-the-difference-and-when-to-use-each

(编辑:李大同)

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

    推荐文章
      热点阅读