Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import Combine
- class Player {
- var score: Int = 0
- }
- let updateScoreNotificationName = Notification.Name("update_score")
- // Create Publisher
- let updateScorePublisher = NotificationCenter.Publisher(center: .default,
- name: updateScoreNotificationName,
- object: nil).map { (notification) -> Int in
- return (notification.object as? Int ?? 0)
- }
- // Create Suscriber Subject
- let updateScoreSuscriberSubject = PassthroughSubject<Int, Never>()
- var player = Player()
- updateScoreSuscriberSubject.assign(to: \.score, on: player)
- // Suscribe
- updateScorePublisher.subscribe(updateScoreSuscriberSubject)
- // Test
- NotificationCenter.default.post(name: updateScoreNotificationName, object: 110)
- print("Player score is: \(player.score)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement