gpt4 book ai didi

swift - Swift 的简单闭包示例

转载 作者:搜寻专家 更新时间:2023-11-01 06:34:08 25 4
gpt4 key购买 nike

我正在学习 swift,我想简单地在函数内部调用闭包。我使用以下内容:

var task : () -> Void
task = {
print("Test")
}

func myFunc(times: Int, task: () -> Void){

task()
}

它应该打印“Test”,但它什么也没做。我错过了什么?

最佳答案

它会这样工作:

var task : () -> Void

task = {

print("Test")
}


func myFunc(times: Int, task: () -> Void){

task()
}

//call your function this way.
myFunc(times: 2, task: task) //this will print "Test" in console.

关于swift - Swift 的简单闭包示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43558543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com