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
})
但显然,尾随闭包语法看起来更清晰。
说明:
- 第一部分
()
表示“没有参数的函数”, - 第二部分
-> ()
表示“无返回值”。
关于快速参数类型 () -> (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33639396/