apiFunc(user: User.currentUser, start: 0, limit: Constants.numberOfItemInOnePage,
success: { [weak self] (friends) -> Void in
dispatch_async(dispatch_get_main_queue(), { [weak self] () -> Void in
if let strongSelf = self {
strongSelf.friendList = friends
strongSelf.loading = false
strongSelf.tableView.reloadData()
}
})
}, failure: nil)
错误
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
编译上面的代码时会发生,但是如果我删除第二个 [weak self],错误就会消失
apiFunc(user: User.currentUser, start: 0, limit: Constants.numberOfItemInOnePage,
success: { [weak self] (friends) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if let strongSelf = self {
strongSelf.friendList = friends
strongSelf.loading = false
strongSelf.tableView.reloadData()
}
})
}, failure: nil)
我觉得因为有2个闭包,所以应该是2个[weak self],谁知道为什么会出现编译错误
最佳答案
您不必像 @weakify
或 __weak self
模式一样在嵌套闭包中重复 [weak self]
目标-C。
[weak self]
在 Swift 中由编译器自动创建相同的模式,weak self 由外部闭包定义,由内部闭包使用。
这是针对 Objective-C 版本的相应问题: iOS blocks and strong/weak references to self
关于swift - 快速关闭时 gcd 中的弱 self ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28780737/