I have added an observer
override func viewDidLoad()
{
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"selector name", name: "observer name", object:nil)
...
}
When removing observer in deinit,
deinit
{
NSNotificationCenter.defaultCenter().removeObserver(self, forKeyPath: <some string>)
}
the app sometimes crashes:
Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer "class" for the key path "some string" from NSNotificationCenter because it is not registered as an observer.
So I am trying to add do/catch
deinit
{
do{
try NSNotificationCenter.defaultCenter().removeObserver(self, forKeyPath: <some string>)
}catch{}
}
But I get a warning:
catch block is unreachable because no errors are thrown in do block
And the app crashes
and when I am adding a try
deinit
{
do{
try NSNotificationCenter.defaultCenter().removeObserver(self, forKeyPath: <some string>)
}catch{}
}
I get this warning:
no calls to throwing functions occur within try expresion
And the app crashes
How should that be done?