【发布时间】:2018-10-11 16:02:56
【问题描述】:
我想在 Swift 闭包中理解 self。
对于前 -
() -> Void = { [weak self] in
guard let `self` = self else {
self.callMethod2()
}
self.callMethod3()
}
为什么我们在这里使用反引号? 这是良好的编程习惯吗? 这里的self怎么被弱捕获了?
【问题讨论】:
-
请注意,soon you'll be allowed to say
guard let self = self else {...}没有反引号 - 解析器会将self视为该上下文中的普通标识符,允许它被反弹:)