Last active
October 18, 2021 14:05
-
-
Save xmhafiz/6dc8d3569ed468f78584bfa18bfd41dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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