Make self weak in methods in Swift

后端 未结 5 2006
遇见更好的自我
遇见更好的自我 2020-12-05 07:06

I have a Swift class that needs to store a table of its own methods. Unfortunately this is causing a reference cycle, because its table retains references to self

5条回答
  •  鱼传尺愫
    2020-12-05 07:49

    In Swift 4 (I am not sure when the syntax became available), simply do { [weak self] (params) in } to make self weak. It basically is to [unowned self], what Self? is to Self!. The compiler even requires self?.foo instead of simply self.foo.

提交回复
热议问题