2017-07-06 8 views
-2

私はそれを修正しようとするたびにこのエラーが発生します。未解決のインディケータ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

'{'と '}' '(jsonResult!= nil){'が宣言の外側にあるかどうかを調べてください。そのコードを 'print'のところに置きます。 – Larme

+0

if(jsonResult!= nil){//エラーがあります。 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) 
} 
関連する問題