方式一:try方式 程序員手動捕捉異常
do {
try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
} catch {
// error異常的對象
print(error)
}
方式二:try?方式(常用方式) 系統(tǒng)幫助我們處理異常,如果該方法出現(xiàn)了異常,則該方法返回nil.如果沒有異常,則返回對應(yīng)的對象
guard let anyObject = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) else {
return
}
方式三:try!方法(不建議,非常危險) 直接告訴系統(tǒng),該方法沒有異常.注意:如果該方法出現(xiàn)了異常,那么程序會報錯(崩潰)
let anyObject = try!NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)