Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 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 Publisher Subscriber
  18. var player = Player()
  19. let updateScoreSubscriber = Subscribers.Assign(object: player, keyPath: \.score)
  20.  
  21. // Subscribe
  22. updateScorePublisher.subscribe(updateScoreSubscriber)
  23.  
  24. // Test
  25. NotificationCenter.default.post(name: updateScoreNotificationName, object: 110)
  26. print("Current player scroe is : \(player.score)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement