1

I'm adding new features with Swift to Objective-C app.

I have this observer in Objective-C (registration.m):

[[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(confirmSms) name:NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS object:nil];

and in confirm.m:

[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS object:nil];

How to observe this in Swift? I've tried

NotificationCenter.default.removeObserver(self,
                                                  name: NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS,
                                                  object:nil);

NotificationCenter.default.addObserver(self,
                                              selector:#selector(self.confirmationSmsSent),
                                              name: NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS,
                                              object: nil);

And I'm getting

Use of unresolved identifier 'NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS'

Thanks

// EDIT:

I have declared in Obj-C:

NSString *const NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS = @"confirmationSMSSent";

Will this still work with this?

let name: NSNotification.Name = NSNotification.Name("Your_Notification_Name_Key_String") //NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS

And when I use

NotificationCenter.default.addObserver(self, selector:#selector(self.confirmationSmsSent(_:)), name: name, object: nil)

func confirmationSmsSent(notification: NSNotification) {

}

I got error

Value of type 'MyController' has no member 'confirmationSmsSent'

on

NotificationCenter.default.addObserver(self, selector:#selector(self.confirmationSmsSent(_:)), name: name, object: nil)

Cœur
  • 37,241
  • 25
  • 195
  • 267
David
  • 825
  • 10
  • 33

3 Answers3

3

In Swift 3, the syntax is changed. You have to define the variable of NSNotification.Name

let name: NSNotification.Name = NSNotification.Name("Your_Notification_Name_Key_String") //NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS

//Add Notification
NotificationCenter.default.addObserver(self, selector:#selector(self.yourSelector(_:)), name: name, object: nil)

//Remove Notification Observer
NotificationCenter.default.removeObserver(self, name: name, object: nil)

//Your Selector
func yourSelector(_ notification: Notification) {
//Code
}
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
  • and self.yourSelector(_:) shoud be defined as func yourSelector()? What should be in () as param? On func confirmationSmsSent(notification: NSNotification) {} I'm getting on the selector error **Value of type 'MyController' has no member 'confirmationSmsSent'** – David Feb 03 '17 at 09:46
  • 1
    @sohil-r-memon Thank you! – David Feb 03 '17 at 13:03
1

This is because you not yet declared NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS.

Normally notification name is just a string, btw you must cast it to nested NSNotification.Name type.

let NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS = NSNotification.Name("<some string>")
Anton Malmygin
  • 3,406
  • 2
  • 25
  • 31
1

The most useful swift - lije code is extension

extension Notification.Name {
static let someNewName = "ThatsItImAwsome" 
} 

Usage inside post :

.someNewKey

this:

NotificationCenter.default.addObserver(self, selector:#selector(self.yourSelector(_:)), name: .someNewKey, object: nil)
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194