如何在 Objective-C 中添加观察者并在 Swift 中触发通知 3

How to add an observer in Objective-C and fire a Notification in Swift 3

我在 Objective C 文件中有一个通知观察器,我想在 Swift 3 文件中触发通知。由于在 Objective C 中通知名称是 NSString 但在 Swift 3 中它是 Notification.Name,我怎样才能让 Obj-C 观察者捕捉到 Swift 火?

Swift 触发器

NotificationCenter.default.post(name: .notificationName, object: nil, userInfo: nil)

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

Obj-C 观察者

[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(displayError:)
                                      name:"Test"
                                      object:nil];

您将像这样触发通知:

NotificationCenter.default.post(
    name: Notification.Name(rawValue: "SomeNotification"),
    object: nil,
    userInfo: nil
)