在看 Raywenderlich 的 RxSwift 教材,有一个写法看不懂

2017-09-30 11:36:54 +08:00
 zenghaojim33
https://gist.github.com/zenghaojim33/a6990148ec50f615a9ded22d3d3ed554
4255 次点击
所在节点    iDev
12 条回复
zenghaojim33
2017-09-30 11:37:36 +08:00
这里中括号后面的(self)是什么意思?_?
guitarkitten
2017-09-30 11:46:30 +08:00
{ this in .... }(self) 这不就是一个调用么?
abcyuxue123
2017-09-30 13:04:06 +08:00
感觉 ls +1

可以去掉 self 尝试运行,结果应该一样
zenghaojim33
2017-09-30 14:12:49 +08:00
@guitarkitten 还是没理解。这个 in 前面的 this 对应的就是(self)吗?

@abcyuxue123 去掉 self 是运行不了的,会提示 this 没有定义
NxiJSiOS
2017-09-30 14:16:50 +08:00
Since self is a struct you can ’ t create weak or unowned references. Instead, pass self to the closure or function that initialized the lazy variable.
zenghaojim33
2017-09-30 14:50:38 +08:00
@NxiJSiOS 貌似明白了点,就是在 struct 里面直接使用 self 的话会报错必须这样传进去?
engHacker
2017-09-30 15:06:51 +08:00
showme1121
2017-09-30 22:00:26 +08:00
@zenghaojim33 this 是整个 closure 的参数啊
zenghaojim33
2017-10-01 16:29:03 +08:00
@showme1121 但是( self )又是什么
error
2017-10-11 17:30:11 +08:00
closure 的调用吧
shawndev
2017-10-15 12:06:18 +08:00
lazy var button : UIButton = {
$0.hide = true
}(UIButton())

和这种写法是一样的
zenghaojim33
2017-10-16 18:47:59 +08:00
@shawndev 明白了,那如果把 UIButton()放到闭包里面是不是一样的?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/394821

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX