Q: 在Swift 3中,是否可以将函数作为函数参数传递?
为了重构我的代码,我想要一个全局函数。在这个函数的结尾,有一个具有action
参数的自定义按钮。
let deleteButton = MIAlertController.Button(title: "cancel",
type: .cancel,
config: getDestructiveButtonConfig(),
action: {
print("completed")
})
我想要做的是,设置一个带有函数参数的全局函数。
MyGlobalStuff.swift
...
func getButton(_ myFunc: func, _ title: String) {
let deleteButton = MIAlertController.Button(title: title,
type: .cancel,
config: getDestructiveButtonConfig(),
action: {
myFunc // call here the function pass by argument
})
}
getButton(..)
在同一类中使用传递的函数调用。
MyViewController.swift
...
getButton(myPrintFunc, "cancel")
func myPrintFunc() {
print("completed")
}
有类似这样的可能性吗?非常感谢您的帮助。