स्विफ्ट 4.0 और Xcode 9.0+:
भेजें (पोस्ट) अधिसूचना:
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
या
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil, userInfo: ["Renish":"Dadhaniya"])
प्राप्त करें (प्राप्त करें) सूचना:
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
सूचना प्राप्त करने के लिए कार्य-विधि हैंडलर:
@objc func methodOfReceivedNotification(notification: Notification) {}
स्विफ्ट 3.0 और Xcode 8.0+:
भेजें (पोस्ट) अधिसूचना:
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
प्राप्त करें (प्राप्त करें) सूचना:
NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
प्राप्त अधिसूचना के लिए विधि हैंडलर:
func methodOfReceivedNotification(notification: Notification) {
// Take Action on Notification
}
अधिसूचना निकालें:
deinit {
NotificationCenter.default.removeObserver(self, name: Notification.Name("NotificationIdentifier"), object: nil)
}
स्विफ्ट 2.3 और Xcode 7:
भेजें (पोस्ट) अधिसूचना
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
प्राप्त (प्राप्त) अधिसूचना
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"NotificationIdentifier", object: nil)
अधिसूचना प्राप्त करने के लिए विधि हैंडलर
func methodOfReceivedNotification(notification: NSNotification){
// Take Action on Notification
}
ऐतिहासिक Xcode संस्करणों के लिए ...
भेजें (पोस्ट) अधिसूचना
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
प्राप्त (प्राप्त) अधिसूचना
NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification:", name:"NotificationIdentifier", object: nil)
अधिसूचना निकालें
NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationIdentifier", object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self) // Remove from all notifications being observed
अधिसूचना प्राप्त करने के लिए विधि हैंडलर
func methodOfReceivedNotification(notification: NSNotification) {
// Take Action on Notification
}
@Objc के साथ या तो कक्षा या लक्ष्य विधि का उल्लेख करें
@objc private func methodOfReceivedNotification(notification: NSNotification) {
// Take Action on Notification
}
// Or
dynamic private func methodOfReceivedNotification(notification: NSNotification) {
// Take Action on Notification
}