【发布时间】:2020-02-01 23:49:50
【问题描述】:
一旦我按下按钮并调用函数,我试图从 url 获取数据,但是一旦调用函数,我就会不断收到 typeMismatch 错误。
这是我的代码:
struct User: Decodable {
var symbol: String
var price: Double
}
struct Response: Decodable {
var results:[User]
}
struct ContentView: View {
var body: some View {
VStack {
Text("hello")
Button(action: {
self.fetchUsers(amount: 0)
}) {
Text("Button")
}
}
}
func fetchUsers(amount: Int) {
let url:URL = URL(string: "https://api.binance.com/api/v3/ticker/price")!
URLSession.shared.dataTask(with: url) { (data, res, err) in
if let err = err { print(err) }
guard let data = data else { return }
do {
let response = try JSONDecoder().decode(Response.self, from: data)
print(response.results[0])
} catch let err {
print(err)
}
}.resume()
}
}
这是错误:
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
我试图从中获取数据的网站网址是: https://api.binance.com/api/v3/ticker/price
我试图从特定符号中获取特定价格,例如 ETHBTC 的价格,即 0.019...
谢谢
【问题讨论】:
标签: swiftui xcode11 swift5 type-mismatch urlsession