How do you create custom notifications in Swift 3?

前端 未结 13 582
自闭症患者
自闭症患者 2020-12-04 09:07

In Objective-C, a custom notification is just a plain NSString, but it\'s not obvious in the WWDC version of Swift 3 just what it should be.

相关标签:
13条回答
  • 2020-12-04 09:38

    This is just reference

    // Add observer:
    NotificationCenter.default.addObserver(self,
        selector: #selector(notificationCallback),
        name: MyClass.myNotification,
        object: nil)
    
        // Post notification:
        let userInfo = ["foo": 1, "bar": "baz"] as [String: Any]
        NotificationCenter.default.post(name: MyClass.myNotification,
            object: nil,
            userInfo: userInfo)
    
    0 讨论(0)
提交回复
热议问题