CoinGeckoのAPIを使ってJSONを取得し表示したい
SwiftでCoinGeckoのWEBAPIを使って価格を表示したい
発生している問題・エラーメッセージ
libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
class AppDelegate: UIResponder, UIApplicationDelegate { Thread 1:signal SIGABRT
該当のソースコード
Swift
1import UIKit 2 3struct Nem: Codable { 4 let nem: Child 5} 6 7struct Child: Codable { 8 let jpy: String 9} 10 11class ViewController: UIViewController { 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 // Do any additional setup after loading the view. 16 func get(){ 17 let urlString = "https://api.coingecko.com/api/v3/simple/price?ids=nem&vs_currencies=jpy" 18 19 guard let url = URLComponents(string: urlString) else { return } 20 21 let task = URLSession.shared.dataTask(with: url.url!) {(data, response, error) in 22 if (error != nil) { 23 print(error!.localizedDescription) 24 } 25 guard let _data = data else { return } 26 27 let Coin = try? JSONDecoder().decode(Nem.self, from: _data) 28 29 let encoder = JSONEncoder() 30 encoder.outputFormatting = .prettyPrinted 31 let encoded = try! encoder.encode(Coin) 32 print(String(data: encoded, encoding: .utf8)!) 33 } 34 } 35 } 36}
補足情報
Xcode 10.3
Swift 5.0.1
Mac Mojave
Swift初心者です。
初歩的なエラーかもしれませんがよくわかりません。
どこを直せば良いのでしょうか。
あなたの回答
tips
プレビュー