Skip to content

Instantly share code, notes, and snippets.

@max6363
Created January 20, 2022 12:38
Show Gist options
  • Save max6363/d9a8a9fc724ad2fb2e61ba67842d4f60 to your computer and use it in GitHub Desktop.
Save max6363/d9a8a9fc724ad2fb2e61ba67842d4f60 to your computer and use it in GitHub Desktop.
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