How to properly use NSNotificationCenter (addObserver) in Swift: understanding syntax and unsubscription
Hello! I've just started learning Swift and am trying to implement data exchange between two ViewControllers using NSNotificationCenter. In Objective-C, I saw this syntax:
```objc
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"MyNotification" object:nil];
```
But in Swift, I got confused. I'm trying to write it like this:
```swift
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification:", name: "MyNotification", object: nil)
```
But Xcode complains about the selector — it says I need to use #selector. I rewrote it:
```swift
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleNotification), name: "MyNotification", object: nil)
```
But now I don't understand how to properly declare the meth
No answers yet.
Log in to leave an answer