{someclass()。func()}()`语法在迅速声明中的意思是什么?

发布于 2025-02-04 12:30:43 字数 229 浏览 1 评论 0原文

我在某个地方找到了它,并且一直在使用它的代码。

我不确定为什么牙套和尾随的帕伦斯。他们有必要吗?他们做什么?我不确定在Swift Docs中查找的地方找到它。

var textField = { UITextField.textFieldWithInsets(insets: UIEdgeInsets(top:0, left:5, bottom:0, right:15)) }()

I found this somewhere and have been using it my code.

I'm not sure why the braces and the trailing parens. Are they necessary? What do they do? I'm not sure where to look in the Swift docs to find it.

var textField = { UITextField.textFieldWithInsets(insets: UIEdgeInsets(top:0, left:5, bottom:0, right:15)) }()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咿呀咿呀哟 2025-02-11 12:30:43

封闭的牙套和尾随的帕伦斯定义a 闭合并立即将其返回值分配给textfield变量。这个单圈等同于写作

var textField = {
    return UITextField.textFieldWithInsets(insets: UIEdgeInsets(top:0, left:5, bottom:0, right:15))
}()

,因为闭合仅包含一行, 返回可以省略关键字,然后将闭合挤压到标志线上。

在这种情况下,闭合定义和调用是完全多余的,这等同于仅写作,

var textField = UITextField.textFieldWithInsets(insets: UIEdgeInsets(top:0, left:5, bottom:0, right:15))

但是,将闭合结果分配到变量的结果是一种分配创建和配置变量结果的不可用的方法跨多行到结果变量,例如

var textField: UITextField = {
    let field = UITextField()
    field.font = ...
    field.textColor = ...
    field.translatesAutoresizingMasksIntoConstraints = false
    return field
}()

,如果textfield local variable(因为您可以创建变量,然后配置它),但它是冗余的,但是在将默认值分配给实例变量时常见,以在初始化器之外执行一致的设置和初始化。例如:

class MyComponent: UIView {
    // Instance variable default values are assigned
    // before initializers are called. If we assign a value here
    // then we don't need to duplicate code across our inits.
    var textField: UITextField = { /* some configuration */ }()
    
    init(text: String) {
        // textField is already initialized and configured.
        textField.text = text
    }

    init(someValue: Int) {
        // textField is already initialized and configured.
        textField.text = someCalculationBased(on: someValue)
    }
}

The enclosing braces and trailing parens define a closure which is immediately called, and whose return value is immediately assigned to the textField variable. This one-liner is equivalent to writing

var textField = {
    return UITextField.textFieldWithInsets(insets: UIEdgeInsets(top:0, left:5, bottom:0, right:15))
}()

Because the closure contains only a single line, the return keyword can be omitted, and the closure was squished onto a signel line.

In this case, the closure definition and call is entirely redundant, and this is equivalent to just writing

var textField = UITextField.textFieldWithInsets(insets: UIEdgeInsets(top:0, left:5, bottom:0, right:15))

However, this style of assigning the result of a closure to a variable is a not-uncommon way to assign the result of creating and configuring a variable across multiple lines to a resulting variable, e.g.

var textField: UITextField = {
    let field = UITextField()
    field.font = ...
    field.textColor = ...
    field.translatesAutoresizingMasksIntoConstraints = false
    return field
}()

This style is also redundant if textField is a local variable (as you can just create the variable, then configure it), but is common when assigning a default value to an instance variable, to perform consistent setup and initialization outside of an initializer. For example:

class MyComponent: UIView {
    // Instance variable default values are assigned
    // before initializers are called. If we assign a value here
    // then we don't need to duplicate code across our inits.
    var textField: UITextField = { /* some configuration */ }()
    
    init(text: String) {
        // textField is already initialized and configured.
        textField.text = text
    }

    init(someValue: Int) {
        // textField is already initialized and configured.
        textField.text = someCalculationBased(on: someValue)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文