gpt4 book ai didi

swift - “self”在所有成员初始化之前被闭包捕获

转载 作者:IT王子 更新时间:2023-10-29 05:41:37 26 4
gpt4 key购买 nike

好吧,下面是我的代码:

import UIKit
import ForecastIO

class Weather {
var temp: Float
var condition: String
var wind: Float
var precip: Float

init() {
DarkSkyClient(apiKey: "<api key>").getForecast(latitude: Utils().getLat(), longitude: Utils().getLong()) { result in

switch result {
case .success(let currentForecast, _):

self.temp = (currentForecast.currently?.temperature)!
self.condition = (currentForecast.currently?.summary)!
self.wind = (currentForecast.currently?.windSpeed)!
self.precip = (currentForecast.currently?.precipitationProbability)!

case .failure(let error):
print(error)

}

}

}

}

所以出现我的错误是因为我试图在 API 调用中初始化 temp。我知道这不是最可靠的方法,但我试图首先让它发挥作用。

第一个错误是:

'self' captured by a closure before all members were initialized

在线 DarkSkyClient(apiKey: "").getForecast(latitude: Utils().getLat(), longitude: Utils().getLong()) { 结果

我的第二个错误:

Return from initializer without initializing all stored properties

倒数第二个

现在,显然我没有正确初始化。我找不到正确的方法来完成我的最终目标。也许我这样做完全错了?

最佳答案

对我来说,这是因为我没有在初始化器中调用 super.init()

class AnObject: NSObject {
override init() {
// super.init()

let _: ()-> (Void) = {
print(String(describing: self))
}
}
}

关于swift - “self”在所有成员初始化之前被闭包捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403488/

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