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
网友评论