系列内容,swift学习路上.
规范目标
- 统一通知的书写.
- 提高通知的代码复用.
- 简化通知的维护.
实现思路
- 定义了通知描述协议,解析协议,统一了通知的书写.
- 定义了通知清除类,实现其在其生命周期结束时自动清除通知的观察者.
实现代码
通知描述协议
//定义通知描述协议包含 name、userInfo、object。
protocol NotificationDescriptor {
static var name: Notification.Name { get }
var userInfo: [AnyHashable: Any]? { get }
var object: Any? { get }
}
extension NotificationDescriptor {
var userInfo: [AnyHashable: Any]? {
return nil
}
var object: Any? {
return nil
}
}
extension NotificationDescriptor {
//发送通知
public func post(on center: NotificationCenter = NotificationCenter.default) {
print(Self.name)
center.post(name: Self.name, object: object, userInfo: userInfo)
}
}
通知数据解析协议
protocol NotificationDecodable {
init(_ notification: Notification)
}
extension NotificationDecodable {
static func observer(on center: NotificationCenter = NotificationCenter.default ,
for aName: Notification.Name,
queue: OperationQueue? = nil,
using block: @escaping (Self) -> Swift.Void) -> NotificationCleaner {
// token 为通知的观察者
let token = center.addObserver(forName: aName, object: nil, queue: queue) { (notice) in
//回调通知,init处理了通知数据.
block(self.init(notice))
}
//返回通知清除类
return NotificationCleaner.init(token, center: center)
}
}
- 注意NotificationDecodable扩展中通知addObserver部分,不需要添加观察者.
- 重点看center.addObserver 通知中心添加观察者的这个方法的文档.
func addObserver(forName name: NSNotification.Name?, object obj: Any?, queue: OperationQueue?, using block: @escaping (Notification) -> Void) -> NSObjectProtocol
block
The block to be executed when the notification is received.
The block is copied by the notification center and (the copy) held until the observer registration is removed.
The block takes one argument: notification
Return Value
An opaque object to act as the observer.
-
参数block: 接收到通知执行
-
返回值为observer, block的生命周期与observer的移除相关.
-
所以,我们的清除类与observer相关.
通知清除类
- 根据生命周期自动清除通知的观察者
class NotificationCleaner {
// 通知的观察者
let token: NSObjectProtocol
let center: NotificationCenter
init(_ token: NSObjectProtocol, center: NotificationCenter) {
self.token = token
self.center = center
}
deinit {
center.removeObserver(token)
}
}
定义通知实现协议集合。
- 方便使用.
typealias TypedNotification = NotificationDescriptor & NotificationDecodable
应用示例
通知名称管理
extension Notification.Name {
static let test = Notification.Name.init("com.typedNotification.test")
}
构建通知
struct TestNotificaiton: TypedNotification {
// 通知名称
static var name: Notification.Name = .test
// 通知信息
var userInfo: [AnyHashable : Any]? {
return [
"passedNum": num,
"passedStr": str
]
}
// 自定义userInfo数据
var num: Int
var str: String
// 初始化数据
init(_ num: Int, str: String) {
self.num = num
self.str = str
}
//收到通知的数据处理
init(_ notification: Notification) {
num = notification.userInfo!["passedNum"] as? Int ?? 0
str = notification.userInfo!["passedStr"] as? String ?? ""
}
}
使用通知
// 通知清除类
var noticeCleaner: NotificationCleaner?
//发送通知
TestNotificaiton.init(1, str: "111").post()
//接收通知
noticeCleaner = TestNotificaiton.observer(for: .test, using: { (notice) in
print(notice.num)
print(notice.str)
})
- 以上为所有内容,代码可以运行,请大家指教.