Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import Foundation
  2. import Combine
  3.  
  4. class Player {
  5. var score: Int = 0
  6. }
  7.  
  8. let updateScoreNotificationName = Notification.Name("update_score")
  9.  
  10. // Create Publisher
  11. let updateScorePublisher = NotificationCenter.Publisher(center: .default,
  12. name: updateScoreNotificationName,
  13. object: nil).map { (notification) -> Int in
  14. return (notification.object as? Int ?? 0)
  15. }
  16.  
  17. // Create Suscriber Subject
  18. let updateScoreSuscriberSubject = PassthroughSubject<Int, Never>()
  19. var player = Player()
  20. updateScoreSuscriberSubject.assign(to: \.score, on: player)
  21.  
  22. // Suscribe
  23. updateScorePublisher.subscribe(updateScoreSuscriberSubject)
  24.  
  25. // Test
  26. NotificationCenter.default.post(name: updateScoreNotificationName, object: 110)
  27. print("Player score is: \(player.score)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement