【问题标题】:How to set a function as function argument in Swift 3如何在 Swift 3 中将函数设置为函数参数
【发布时间】:2016-11-07 14:41:16
【问题描述】:

:是否可以在 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")
}

这样的事情可能吗?非常感谢您的帮助。

【问题讨论】:

  • 你在找这个吗? Function Types as Parameter Types in developer.apple.com/library/content/documentation/Swift/…
  • @Santosh 您的链接不涵盖我的问题。我不是在寻找关闭或返回值
  • 好吧。实际上我需要一个关闭...我发誓,我以前尝试过。不工作。答案后再次尝试,工作......我的错。谢谢你或你的时间

标签: swift


【解决方案1】:

是的,您可以将函数/闭包作为另一个函数的参数传递。

这是语法

func doSomething(closure: () -> ()) {
    closure()
}

这里的函数doSomething 接收一个没有参数和返回类型的闭包作为参数。

你可以用这种语法调用doSomething

doSomething(closure: { _ in print("Hello world") })

或者使用尾随闭包语法

doSomething { 
    print("Hello world")
}

【讨论】:

  • 闭包不起作用,因为这样代码将在调用 getButton 函数的那一刻被调用,而不是在按下按钮后调用
  • @DavidSeek,为什么getButton函数被调用的时候会执行?
  • 如果我在 getButton 中有一个闭包,它将在函数完成时被调用。但我需要在按下按钮后调用它
  • 你把它放在actionMIAlertController.Button的参数中了吗?
  • 好的。让它工作。之前尝试过关闭,但没有奏效。猜我不好。我也认为,您的方法与卢克的方法相同吗? _ myFunc: () -> Void_ closure: () -> () 相同
【解决方案2】:

假设

typealias Func = () -> Void    

你可以这样做:

func getButton(_ title: String, _ myFunc: Func) {
    let deleteButton = MIAlertController.Button(title: title, 
                                                type: .cancel, 
                                                config: getDestructiveButtonConfig(),
                                                action: {
                                                    myFunc()
                                                })
}

甚至:

func getButton(_ title: String, _ myFunc: Func) {
    let deleteButton = MIAlertController.Button(title: title, 
                                                type: .cancel, 
                                                config: getDestructiveButtonConfig(),
                                                action: myFunc
                                                )
}

另外,如果你没有注意到,我将闭包移到了参数列表的末尾。这样,它将允许高级魔法,例如:

getButton("X") { … }

而不是

getButton({ … }, "X")

【讨论】:

  • 呃。我喜欢 typealias。谢谢你!对此表示赞同
【解决方案3】:

函数是 swift 中的第一类,所以你可以像这样传递它们:

func getButton(_ myFunc: () -> Void, _ title: String) {

需要在类型中指定函数的签名,即参数类型和返回类型

【讨论】:

  • 谢谢,您的方法与 appzYourLife 的方法一样有效。给他打勾,因为他跑得更快。为您的时间和帮助投票。谢谢
猜你喜欢
  • 2013-11-26
  • 1970-01-01
  • 2017-11-04
  • 2017-10-05
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 2017-03-15
相关资源
最近更新 更多