本文介绍了通过typealias快速关闭无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么typealias闭包不传输数据并且什么也不输出到控制台?
why does typealias closure not transmit data and output nothing to the console? How to fix it?
class viewModel: NSObject {
var abc = ["123", "456", "789"]
typealias type = ([String]) -> Void
var send: type?
func createCharts(_ dataPoints: [String]) {
var dataEntry: [String] = []
for item in dataPoints {
dataEntry.append(item)
}
send?(dataEntry)
}
override init() {
super.init()
self.createCharts(abc)
}
}
class ViewController: UIViewController {
var viewModel: viewModel = viewModel()
func type() {
viewModel.send = { item in
print(item)
}
}
override func viewDidLoad() {
super.viewDidLoad()
print("hello")
type()
}
}
我有一个类似设计的项目,但我无法重复
I have a project in which a similar design works, but I can not repeat it
推荐答案
该模式很好,但是时间不对。
The pattern is fine, but the timing is off.
您在<$ c $期间调用 createCharts
c> init 视图模型。但是在完成视图模型的 init
后,视图控制器将设置 send
关闭。
You’re calling createCharts
during the init
of the view model. But the view controller is setting the send
closure after the init
of the view model is done.
底线,您可能不想在 init createCharts
/ code>视图模型。
Bottom line, you probably don’t want to call createCharts
during the init
of the view model.
这篇关于通过typealias快速关闭无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!