gpt4 book ai didi

swift - 完成和返回值

转载 作者:行者123 更新时间:2023-11-28 14:56:40 24 4
gpt4 key购买 nike

我正在为这段代码苦苦挣扎。我正在尝试返回从 api 链接获得的 json 数据。我阅读了其他几个问题,但没有找到让它发挥作用的方法。所有的 Playground 代码都在这里。提前致谢!

import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

struct WebsiteDescription: Decodable {
let ETH: Devise
}
struct Devise: Decodable {
let EUR : Double
}

func refreshPrices(DeLaDevise nom: String, completion: @escaping ((Double) -> Double)) -> Void {

guard let url = URL(string: "https://min-api.cryptocompare.com/data/pricemulti?fsyms=" + nom + "&tsyms=EUR") else { return }
URLSession.shared.dataTask(with: url) { (data, reponse, error) in
guard let data = data else { return }
var prix: Double = 0.0
print(data)
do {
let website = try JSONDecoder().decode(WebsiteDescription.self, from: data)
prix = website.ETH.EUR
completion(prix)
} catch let jsonErr {
print(jsonErr)
}
}.resume()
}

refreshPrices(DeLaDevise: "ETH") { prix in
print(prix)
}

最佳答案

差不多。

这看起来很矛盾,但是闭包返回一些东西,它传递了一个参数。

错误消息为您提供了准确的线索:

Cannot convert value of type '()' to closure result type 'Double'

只需将返回值更改为 Void 又名 ()

func refreshPrices(DeLaDevise nom: String, completion: @escaping (Double) -> ()) -> Void {

关于swift - 完成和返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49287668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com