{someclass()。func()}()`语法在迅速声明中的意思是什么?
我在某个地方找到了它,并且一直在使用它的代码。
我不确定为什么牙套和尾随的帕伦斯。他们有必要吗?他们做什么?我不确定在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
封闭的牙套和尾随的帕伦斯定义a 闭合并立即将其返回值分配给
textfield
变量。这个单圈等同于写作,因为闭合仅包含一行,
返回
可以省略关键字,然后将闭合挤压到标志线上。在这种情况下,闭合定义和调用是完全多余的,这等同于仅写作,
但是,将闭合结果分配到变量的结果是一种分配创建和配置变量结果的不可用的方法跨多行到结果变量,例如
,如果
textfield
是 local variable(因为您可以创建变量,然后配置它),但它是冗余的,但是在将默认值分配给实例变量时常见,以在初始化器之外执行一致的设置和初始化。例如: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 writingBecause 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
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.
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: