Converting a strong reference to weak reference to self

734 views Asked by At

How do you convert this to a weak reference to self?

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

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

Also, a related question--when does the view controller actually get destroyed? I'm assuming when you tab in and out of different views, view controllers still stay alive right? i.e. They don't get destroyed on viewDidDisappear.

1

There are 1 answers

0
Ajay Singh Thakur On

this is the function in viewcontroller where it actually get out of the momory so here you can remove observer

                deinit {
                    // you code

                }

and for making weak reference of self you can use

                weak var this = self