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

时间:2017-06-07 23:33:15

标签: objective-c swift nsnotificationcenter notificationcenter

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

Swift Trigger

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

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

Obj-C Observer

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

1 个答案:

答案 0 :(得分:-1)

你会触发这样的通知:

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