//swift中提供了三种处理异常的方法:
//方式一,try方式:这种情况下返回any类型
do{
let jsonStr01 = try JSONSerialization.jsonObject(with: jsonData as Data, options: .mutableContainers)
print("没有错误----")
print(jsonStr01)
}catch{
print(error)
}
//方式二,try?方式:这种情况下返回可选类型
let jsonStr02 = try? JSONSerialization.jsonObject(with: jsonData as Data, options: .mutableContainers)
print(jsonStr02 ?? "数据没有正确取出")
//方式三, try!直接返回固定类型
let jsonStr03 = try! JSONSerialization.jsonObject(with: jsonData as Data, options: .mutableContainers)
print(jsonStr03)
网友评论