gpt4 book ai didi

Swift:完成处理程序和单例

转载 作者:行者123 更新时间:2023-11-28 10:18:26 25 4
gpt4 key购买 nike

我正在尝试使用完成处理程序从函数返回信息。该函数在一个类中,我使用单例来创建该类的实例,以便我可以调用该函数。出于某种原因,我可以在使用单例时访问完成处理程序。

class load {
static let class = Class()
}

load.class.function(//The completion handler should appear here but doesn't)

最佳答案

这就是在 Swift 中使用方法和完成处理程序定义 Singleton 的方式

class Singleton {
static let sharedInstance = Singleton()
private init() { }

func retrieveTheAnswer(completion: (answer:Int) -> ()) {
completion(answer: 42)
}
}

这就是你如何使用它

Singleton.sharedInstance.retrieveTheAnswer { (answer) in
print(answer)
}

关于Swift:完成处理程序和单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38357707/

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