3 回答

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
NSJSONSerialization
do { if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary { print(jsonResult) }} catch let error as NSError { print(error.localizedDescription)}
NSJSONSerialization
do { if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] { print(jsonResult) }} catch let error as NSError { print(error.localizedDescription)}

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
error
inout
處理SWIFT中的錯(cuò)誤:在SWIFT中,此方法返回一個(gè)非可選的結(jié)果,并使用拋出關(guān)鍵字標(biāo)記,以指示在發(fā)生故障時(shí)拋出錯(cuò)誤。
您可以在try表達(dá)式中調(diào)用此方法,并處理do語句的CATCH子句中的任何錯(cuò)誤,如SWIFT編程語言中的錯(cuò)誤處理(SWIFT 2.1),以及在使用SWIFT與Cocoa和Object-C(SWIFT 2.1)時(shí)的錯(cuò)誤處理。
try?
nil
let message = try? NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments)if let dict = message as? NSDictionary { // ... process the data}
do/catch
:
do { let message = try NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments) if let dict = message as? NSDictionary { // ... process the data }} catch let error as NSError { print("An error occurred: \(error)")}

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
do{ if let responseObj = try JSONSerialization.jsonObject(with: results, options: .allowFragments) as? NSDictionary{ if JSONSerialization.isValidJSONObject(responseObj){ //Do your stuff here } else{ //Handle error } } else{ //Do your stuff here } } catch let error as NSError { print("An error occurred: \(error)") }
- 3 回答
- 0 關(guān)注
- 727 瀏覽
添加回答
舉報(bào)