Created
January 20, 2022 12:38
-
-
Save max6363/d9a8a9fc724ad2fb2e61ba67842d4f60 to your computer and use it in GitHub Desktop.
This file contains 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
import UIKit | |
import Combine | |
extension Notification.Name { | |
static let newEvent = Notification.Name("new_event") | |
} | |
struct Event { | |
let title: String | |
let scheduledOn: Date | |
} | |
let eventPublisher = NotificationCenter.Publisher(center: .default, name: .newEvent, object: nil) | |
.map { (notification) -> String? in | |
return (notification.object as? Event)?.title ?? "" | |
} | |
let theEventTitleLabel = UILabel() | |
let newEventLabelSubscriber = Subscribers.Assign(object: theEventTitleLabel, keyPath: \.text) | |
eventPublisher.subscribe(newEventLabelSubscriber) | |
let event = Event(title: "Introduction to Combine Framework", scheduledOn: Date()) | |
NotificationCenter.default.post(name: .newEvent, object: event) | |
print("Recent event notified is: \(theEventTitleLabel.text!)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment