How to define array of closures in Swift?

2020-08-13 08:09发布

问题:

I want to define like this:

public var reloadFRCsNeedToPerformWhenFail = [()->()]()

but I get an error

回答1:

Like this:

public var reloadFRCsNeedToPerformWhenFail : [()->()] = []

If you use a type alias to make ()->() a type, you can do it your way:

public typealias VoidVoid = ()->()
public var reloadFRCsNeedToPerformWhenFail = [VoidVoid]()

Or, forego the [] shortcut notation and use the full generic:

public var reloadFRCsNeedToPerformWhenFail = Array<()->()>()