NotificationCenter not always calls observer when specified object is a String

I have been trying to check why my observer is sometimes called and sometimes isn’t when specifying a String object to filter out notifications.

My original code:

NotificationCenter.default.addObserver(self, selector: #selector(update), name: .pathContentsChanged, object: self.folder.path) // Where path is something like "/user/folder"  @objc func update() {     // Not always called } 

My current workaround, which confirms that the notification object does match self.folder.path:

NotificationCenter.default.addObserver(self, selector: #selector(pathContentsChanged(_:)), name: .pathContentsChanged, object: nil) // No object specified anymore  @objc func pathContentsChanged(_ notification: Notification) {     // This does get called every time     guard let path = notification.object as? String,         path == self.folder.path else     {         return     }     self.update() // notification.object and self.folder.path do match } 

Is it inherent at how Strings work on Swift? Is it because notification.object could be a bridged NSString?

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.