How can you capture multiple arguments weakly in a Swift closure?

南楼画角 提交于 2019-12-08 14:30:19

问题


Is there a way to capture multiple arguments weakly in a swift closure? I know this is the syntax to capture one argument weakly:

{ [weak arg]
    arg.doSomething()
}

How can I do this for two objects that I wish to capture weakly?


回答1:


From Expressions in "The Swift Programming Language" (emphasis added):

Closure Expression
...
A closure expression can explicitly specify the values that it captures from the surrounding scope using a capture list. A capture list is written as a comma separated list surrounded by square brackets, before the list of parameters. If you use a capture list, you must also use the in keyword, even if you omit the parameter names, parameter types, and return type.

Example:

{
    [weak arg1, weak arg2] in 
    // ...
}


来源:https://stackoverflow.com/questions/28015394/how-can-you-capture-multiple-arguments-weakly-in-a-swift-closure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!