Created
April 3, 2022 09:08
-
-
Save paigeshin/d77d64b9e5025b7d179eaf059103bf04 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
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