ios - 将强引用转换为对 self 的弱引用

标签 ios swift swift2 weak-references strong-references

如何将其转换为对 self 的弱引用?

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyViewController.handleMessage(_:)), name: "NewMessage", object: nil)

NSNotificationCenter.defaultCenter().removeObserver(self, name: "NewMessage", object: nil)

还有一个相关的问题—— View Controller 什么时候真正被销毁?我假设当您进出不同的 View 时, View Controller 仍然保持事件状态,对吧?即它们不会在 viewDidDisappear 上被销毁。

最佳答案

这是 View Controller 中的函数,它实际上是从内存中取出的,所以在这里你可以删除观察者

                deinit {
                    // you code

                }

为了对自己进行弱引用,您可以使用

                weak var this = self

关于ios - 将强引用转换为对 self 的弱引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41189240/

相关文章:

ios - 无法将 IBAction 连接到 XIB 中的 UITextField (Swift)

swift - Firebase 检查帐户是否被禁用

ios - 在ISO-8859-1中编码NSURL

ios - RGBA 的 CGColorSpace 在哪里?

ios - 以编程方式添加按钮会导致 fatal error

ios - 处理 iOS 中动态 View 的所有旋转

ios - NSMutableArray 数据按距离排序

ios - 如何从@objc 函数在 UIViewController 之间传递数据数组

Swift 2.0 : NSRangeException, 范围或索引越界

ios - '调用中的额外参数 'id'' 与 Swift 结构