// NSNotification.Name拡張 extension NSNotification.Name { static let sample = Notification.Name(rawValue: "sample") // static let hoge = Notification.Name(rawValue: "hoge") // static let fuga = Notification.Name(rawValue: "fuga") } ///////////////////////////////////////////////////////////////////////////////////////////// // 通知発行側コード class DetailViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } @IBAction private func onNotifyButtonClick(_ sender: UIButton) { // NotificationCenter.default.post(name: NSNotification.Name(rawValue: "sample"), object: nil, userInfo: nil) NotificationCenter.default.post(name: .sample, object: nil) } } ///////////////////////////////////////////////////////////////////////////////////////////// // 通知受信側コード class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // NotificationCenter.default.addObserver(self, selector: #selector(onSampleNotified(_:)), name: NSNotification.Name(rawValue: "sample"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(onSampleNotified(_:)), name: .sample, object: nil) } deinit { // NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "sample"), object: nil) NotificationCenter.default.removeObserver(self, name: .sample, object: nil) } func onSampleNotified(_ notification: NSNotification) { print("Notification button pushed") } }