DispatchQueue.main.asyncでjsonを参照する。
override func viewDidLoad() {
super.viewDidLoad()
let stringUrl = "http://hpscript.com/swift/index.php"
do {
guard let url = URL(string: stringUrl) else { return }
URLSession.shared.dataTask(with: url){(data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
let json = try! JSONDecoder().decode([JsonSample].self, from: data)
DispatchQueue.main.async {
self.label.text = json[0].name //←ここでjsonを参照する
}
}.resume()
} catch{
}
}
jsonはcodableを使う。
なるほど!行けるか???行ければandroidへ直ぐにGo