Ios 在函数声明中将函数设置为变量
我希望在函数声明中将函数作为变量,然后在声明的函数中调用该变量。如何在Swift中执行此操作? 伪代码:Ios 在函数声明中将函数设置为变量,ios,swift,watchos,Ios,Swift,Watchos,我希望在函数声明中将函数作为变量,然后在声明的函数中调用该变量。如何在Swift中执行此操作? 伪代码: let something = 0 func one() { print("one") } // Definition func two( funcVariable: Void, number: Int) { print("\(number)") funcVariable() // here I want to call variable function } /
let something = 0
func one() {
print("one")
}
// Definition
func two( funcVariable: Void, number: Int) {
print("\(number)")
funcVariable() // here I want to call variable function
}
// Call
two(funcVariable: one(), number: somethhing)
怎么做?
示例代码很受欢迎。以下是您的操作方法:
let something = 0
func one() {
print("one")
}
// Definition
func two(funcVariable: () -> Void, number: Int) {
print("\(number)")
funcVariable()
}
// Call
two(funcVariable: one, number: something)