Skip to content

Instantly share code, notes, and snippets.

@paigeshin
Created April 3, 2022 09:08
Show Gist options
  • Save paigeshin/d77d64b9e5025b7d179eaf059103bf04 to your computer and use it in GitHub Desktop.
Save paigeshin/d77d64b9e5025b7d179eaf059103bf04 to your computer and use it in GitHub Desktop.
extension Notification.Name {
static let taskAddedNotification = Notification.Name("TaskAddedNotification")
}
struct ListenToNotificationChanges: View {
@State private var newTask: String?
var body: some View {
VStack {
Button("Post Notification") {
NotificationCenter.default.post(name: Notification.Name.taskAddedNotification, object: "Wash the car")
}
Text(newTask ?? "no task received")
.onReceive(NotificationCenter.default.publisher(for: Notification.Name.taskAddedNotification)) { object in
newTask = object as? String
}
}
}
}
struct ListenToNotificationChanges_Previews: PreviewProvider {
static var previews: some View {
ListenToNotificationChanges()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment