考虑以下代码:
func testA(){}
func testB(value:Int){}
func testC(value:String){}
var someTest:Any = testA
如何对持有闭包的变量进行大小写匹配以找到正确的变量,以便您可以调用它?
switch someTest{
case let test where test:() -> Void:
test()
case let test where test:(value:Int) -> Void:
test(4)
case let test where test:(value:String) -> Void:
test("A")
}
这样的事情可能吗?
答案 0 :(得分:1)
闭包类型与任何其他类型的开关模式相同:
switch someClosure {
case let runnable as () -> Void:
runnable()
case let intConsumer as (Int) -> Void:
intConsumer(4)
case let stringConsumer as (String) -> Void:
stringConsumer("A")
}