快速参数类型 () -> ()

标签 swift types arguments

func speak(text:String, onComplete:()->()) {

    mySpeechUtterance = AVSpeechUtterance(string: text)
    mySpeechSynthesizer.speakUtterance(mySpeechUtterance)

    onComplete()
}

我的问题是:如何调用这个方法?

speechSynthesizer.speak(actions[0], onComplete: "here")

最佳答案

传递一个闭包。

speechSynthesizer.speak(actions.first) {
    // code to be executed after speaking
}

这与

相同
speechSynthesizer.speak(actions.first, onComplete: {
    // code to be executed after speaking
})

但显然,尾随闭包语法看起来更清晰。

说明:

  1. 第一部分()表示“没有参数的函数”,
  2. 第二部分-> ()表示“无返回值”。

关于快速参数类型 () -> (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33639396/

相关文章:

generics - 从函数和方法返回受约束的泛型

ios - 将 Collection View 单元格垂直放置而不是水平放置?

haskell - 如何(或为什么) `Data.Set String` 不是单一类型?

Objective-C 方法语法

Groovy 命名和默认参数

swift - 如何在 Swift 中使用类型删除

cocoa - Twitter OAuth Swift OS X

c# - 获取基本类型的所有最后后代?

javascript - 使用 Flow 检查 React defaultProps

python - 如何在 Python 中使用 getopt/OPTARG?如果给出太多参数(9),如何转换参数?