在swift中,使用strongSelf的正确方式是什么?
在swift中,使用strongSelf的正确方式是什么?
在Objective-C中,在复杂的闭包中,我注意到使用weakSelf/strongSelf。
在Swift中使用strongSelf的正确方法是什么?
if let strongSelf = self { strongSelf.doSomething() }
因此,在闭包中包含self的每一行都应该添加strongSelf检查吗?
if let strongSelf = self { strongSelf.doSomething1() } if let strongSelf = self { strongSelf.doSomething2() }
有没有更优雅的方法来实现上述操作?
admin 更改状态以发布 2023年5月24日
Swift 4.2
guard let self = self else { return }
参考:https://benscheirman.com/2018/09/capturing-self-with-swift-4-2/
参考2:https://www.raywenderlich.com/5370-grand-central-dispatch-tutorial-for-swift-4-part-1-2