I have an observer of NSNotification
which is called twice. I do not know what to do with it.
I googled it but no solution found.
[[NSNo
For those looking for a solution in Swift 2.2 and above and who have reached this question like me you can create an extension as follows :
import Foundation
extension NSNotificationCenter {
func setObserver(observer: AnyObject, selector: Selector, name: String?, object: AnyObject?) {
NSNotificationCenter.defaultCenter().removeObserver(observer, name: name, object: object)
NSNotificationCenter.defaultCenter().addObserver(observer, selector: selector, name: name, object: object)
}
}
You can call this method as follows :
NSNotificationCenter.defaultCenter().setObserver(self, selector: #selector(methodName), name: "name", object: nil)
The extension will handle the removal of previous observer if it exists. Even if there was no previous observer present this code won't crash.