zoukankan      html  css  js  c++  java
  • Swift处理异常的三种方式-try

     1 //  方式一:try方式 程序员手动捕捉异常
     2         do {
     3                 try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
     4             } catch {
     5                 // error异常的对象
     6                 print(error)
     7             }
     8 
     9 //  方式二:try?方式(常用方式) 系统帮助我们处理异常,如果该方法出现了异常,则该方法返回nil.如果没有异常,则返回对应的对象
    10     guard let anyObject = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) else {
    11         return
    12     }
    13 
    14 //  方式三:try!方法(不建议,非常危险) 直接告诉系统,该方法没有异常.注意:如果该方法出现了异常,那么程序会报错(崩溃)
    15      let anyObject = try!NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
  • 相关阅读:
    python调用ggsci.exe程序
    confluence安装
    nginx优化
    ELKstack搭建
    zabbix 安装
    python requests
    小程序消息推送
    shell
    rar 解压
    ubuntu 安装部分设置U盘启动系统安装盘操作
  • 原文地址:https://www.cnblogs.com/panda1024/p/6283100.html
Copyright © 2011-2022 走看看