如何在Swift闭包中弱捕获多个参数? [英] How can you capture multiple arguments weakly in a Swift closure?

查看:111
本文介绍了如何在Swift闭包中弱捕获多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在快速关闭中弱地捕获多个参数?我知道这是弱捕获一个参数的语法:

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()
}

如何对希望弱捕获的两个对象执行此操作? / p>

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

推荐答案

来自表达式(强调):


关闭表达式

...

闭包表达式可以使用捕获列表显式指定
从周围范围捕获的值。捕获的
列表被写为逗号分隔的列表,并在参数列表之前用方括号
括起来。如果使用捕获列表
,则即使省略
参数的名称,参数类型和返回类型,也必须使用 in 关键字。

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.

示例:

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

这篇关于如何在Swift闭包中弱捕获多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆