在函数声明中将函数设置为变量
Set function as variable in function declaration
我想在函数声明中将函数作为变量,而不是在声明的函数中调用该变量。如何在 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
}
// 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)
我想在函数声明中将函数作为变量,而不是在声明的函数中调用该变量。如何在 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
}
// 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)