import UIKit import Combine //declare notification let notificationName = Notification.Name("MyNotification") let param1 = "Param1" // declare publisher // We need this in classes where a publisher needs to live longer than the function it is defined var cancellables = Set() let publisher = NotificationCenter.default.publisher(for: notificationName) publisher.map({notification -> String in guard let userInfo = notification.userInfo else { return "Error : \(notification.name) has no userInfo" } guard let value = userInfo[param1] else { return "Error: \(notification.name) has no or wrong parameter" } return " Value : \(value)" }) .sink(receiveValue: { valueString in print(valueString) }).store(in: &cancellables) // now send notifications from anywhere NotificationCenter.default.post(name:notificationName,object: nil,userInfo: nil) NotificationCenter.default.post(name: notificationName,object:nil,userInfo: ["Dummy":122]) NotificationCenter.default.post(name: notificationName,object:nil,userInfo: [param1:42])
By accepting you will be accessing a service provided by a third-party external to http://h-mayer.de/