美文网首页
Swift-对NotificationCenter进行封装

Swift-对NotificationCenter进行封装

作者: Cy_Star | 来源:发表于2018-08-09 17:16 被阅读14次
final  class NotificationToken: NSObject {
      let notificcationCenter: NotificationCenter
      let token: Any
      
      init(notificationCenter: NotificationCenter = .default, token: Any){
            self.notificationCenter = notificationCenter    
            self.token = token
      }
      
      deinit {
              notificationCenter.removeObserver(token)
      }
}


extension NotificationCenter {
    
        func observe(name: NSNotification.Name?, object obj Any?, queue: OperationQueue?, using block: @escaping (Notification) ->()) -> NotificationToken
        {
              let token = addObserver(forName: name, object: obj, queue: queue, using: block)
              return NotificationToken(notificationCenter: self, token: token)
        }
}

相关文章

网友评论

      本文标题:Swift-对NotificationCenter进行封装

      本文链接:https://www.haomeiwen.com/subject/hrscbftx.html