2017-07-06 50 views
-2

我每次嘗試修復它都會收到此錯誤。請幫助使用未解決的indetifier jsonResult

do { 
    if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] { 
     print(jsonResult) 
    } 
} catch let error as NSError { 
    print(error.localizedDescription) 
} 

if (jsonResult != nil) { 
    print(jsonResult) 
} else { 
    // couldn't load JSON, look at error 
} 
+0

什麼錯誤?我沒有看到錯誤?請更加具體 – Scriptable

+0

看看'{'和'}'if(jsonResult!= nil){'是否在聲明之外。如果(jsonResult!= nil){//錯誤出現在這裏,請將代碼放在你打印的地方'print' – Larme

+0

。 print(jsonResult) –

回答

0

jsonResult在其創建的DO-try-catch語句外使用,所以這就是爲什麼你不能訪問它。這裏是更正的代碼:

do { 
    if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] { 
     print(jsonResult) 
     //The following lines of code is commented out because you have used if let outside to make sure that jsonResult here is not nil. 
     //So you do not need to check for it again 
     //if (jsonResult != nil) { 
     // print(jsonResult) 
     //} else { 
     // // couldn't load JSON, look at error 
     //} 
    } 

} catch let error as NSError { 
    print(error.localizedDescription) 
}