how to use Notification.Name extension from swift 3 to Objective-C

BARS picture BARS · Oct 18, 2017 · Viewed 9.7k times · Source

I created an extension for Notification.Name as below :

public extension Notification.Name {
      public static let blahblahblah = Notification.Name(rawValue: "blahblahblah")

}

Now I want to use this extension in Objective-C, but it's not accessible even if its public.

Can you tell me how to access and use this swift extension in Objective-C and Swift both?

previously I was using constant values in Objective-C, but now for upgrading my code, I wanna use this extension.

Answer

Warif Akhand Rishi picture Warif Akhand Rishi · Jan 23, 2018

My extension in swift file

extension Notification.Name {
    static let purchaseDidFinish = Notification.Name("purchaseDidFinish")
}

@objc extension NSNotification {
    public static let purchaseDidFinish = Notification.Name.purchaseDidFinish
}

// OBJECTIVE-C

#import YourProjectName-Swift.h

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(purchaseDidFinish) name:NSNotification.purchaseDidFinish object:nil];

// SWIFT
NotificationCenter.default.addObserver(self, selector: #selector(purchaseDidFinish), name: .purchaseDidFinish, object: nil)

@objc func purchaseDidFinish(notification: Notification) {
    print("purchaseDidFinish")
}

@leanne's answer was super helpful