NotificationExtension.swift 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //
  2. // NotificationExtension.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/24.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import Foundation
  9. protocol Notifier {
  10. associatedtype Notification:RawRepresentable
  11. }
  12. extension Notifier where Notification.RawValue == String{
  13. fileprivate static func name(for notification:Notification) -> String{
  14. return "\(self).\(notification.rawValue)"
  15. }
  16. func postNotification(_ notification:Notification,object:Any? = nil,userInfo:[String:Any]? = nil) {
  17. }
  18. static func postNotification(_ notification:Notification,object:Any? = nil,userInfo:[String:Any]? = nil){
  19. let aName = name(for: notification)
  20. NotificationCenter.default.post(name: Foundation.Notification.Name(aName), object: object, userInfo: userInfo)
  21. }
  22. static func addObserver(_ observer:Any,selector:Selector,notification:Notification,object:Any? = nil){
  23. let aName = name(for: notification)
  24. NotificationCenter.default.addObserver(observer, selector: selector, name: NSNotification.Name(aName), object: object)
  25. }
  26. static
  27. func removeObserver(_ observer: Any,notification:Notification,object:Any? = nil){
  28. let aName = name(for: notification)
  29. NotificationCenter.default.removeObserver(observer, name: NSNotification.Name(rawValue: aName), object: object)
  30. }
  31. }