尋找傳遞一個函數作爲一個參數(如onCompletion),將作爲Swift函數調用的一部分(類似於JavaScript閉包)調用。這裏最好的做法是什麼?如何將函數作爲函數swift的參數傳遞?
代碼:
func didFinishFunc(onCompletion: func) {
func()
}
func onCompletionFunc()
{
print("completed.")
}
func caller()
{
didFinishFunc(onCompletion: onCompletionFunc)
}
// caller is called
caller()
閱讀swift語言指南。 https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html – Alexander
感謝您的詳細回覆 – KingChintz
我不會浪費時間寫一個精心準備的回答,如果你已經做了盡職調查,並且在來這裏之前閱讀了語言指南或搜索了Google搜索,那麼這些問題很容易回答。你有3個反對票是有原因的。 – Alexander