Skip to content

Instantly share code, notes, and snippets.

@xmhafiz
Last active October 18, 2021 14:05
Show Gist options
  • Save xmhafiz/6dc8d3569ed468f78584bfa18bfd41dd to your computer and use it in GitHub Desktop.
Save xmhafiz/6dc8d3569ed468f78584bfa18bfd41dd to your computer and use it in GitHub Desktop.
// 1
extension Notification.Name {
static let profileUpdated = Notification.Name("OnProfileUpdated")
}
class ViewController: UIViewController {
//...
//...
override func viewDidLoad() {
super.viewDidLoad()
initObserver()
}
// 2
func initObserver() {
// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(self.updateUI(_:)), name: NSNotification.Name.profileUpdated, object: nil)
}
// 3
@objc private func updateUI(_ notification: Notification) {
// 4
guard let userInfo = notification.userInfo else { return }
if let title = userInfo["title"] as? String {
// print title
}
if let subtitle = userInfo["subtitle"] as? String {
// print subtitle
}
if let image = userInfo["image"] as? UIImage {
// do something with the image
}
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment