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

I created an extension for Notification.Nameas 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 is not available, even if it is published.

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

I used to use constant values โ€‹โ€‹in Objective-C, but now I want to use this extension to update my code.

+10
source share
3 answers

Notification.Name Objective-C. NotificationName Objective-C NSString. Swift Objective-C, Swift (, Notification String).

, , :

  • Notification.Name, ; ,
  • Objective-C (, NSString , , NSNotification).

1) Objective-C- Swift:

public extension NSNotification {
    public static let blahblahblah: NSString = "blahblahblah"
}

: Swift 4 Objective-C. :

@objc public extension NSNotification {
    public static var blahblahblah: NSString {
        return "blahblahblah"
    }
}

var computed: , let.

2) Objective C Xcode Swift ( ):

#import "YourProjectName-Swift.h"

: YourProjectName . , "CoolGameApp", Swift "CoolGameApp-Swift.h". , "Cool Game App", : "Cool-Game-App-Swift.h"

3) .

Objective-C:

[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification.blahblahblah object:self];
+10

swift

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

+20

@objc NSNotification , Obj-C (Swift 4).

+5

Source: https://habr.com/ru/post/1687753/


All Articles