Swift利用协议优化NSNotificationCenter

NSNotificationCenter存在的问题

通知没有统一的命名格式

对于通知的命名没有强制的要求,一个项目里可能有多种不同的命名规则。比如:

1
2
3
4
5
6
class Barista {
    let notification = "coffeeMadeNotification"
}
class Trainee {
    let coffeeMadeNotificationName = "Coffee Made"
}

通知名称可能冲突

因为对于通知名称没有限制,可能在不同的对象定义了同样的通知名,这样会导致难以意料的bug。

1
2
3
4
5
6
class Barista {
    let notification = "coffeeMadeNotification"
}
class Trainee {
    let coffeeMadeNotificationName = "coffeeMadeNotification"
}

通知的名称是字符串

字符串真的太危险了,很容易出现打错的情况。

1
2
NSNotificationCenter.defaultCenter()
    .postNotificationName("coffeeMadNotfication")

利用protocol的解决方案

我们通过设计一个protocol来解决上面提到的问题。

1
protocol Notifier { }

通过枚举统一通知名称

给这个协议增加一个关联类型:

1
2
3
protocol Notifier {
    associatedType Notification: RawRepresentable
}

所有要发送通知的对象或者结构体都要实现Notifier这个协议,然后提供一个实现了RawRepresentable的类型。其实就是一个字符串枚举。

1
2
3
4
5
6
class Barista : Notifier {
    enum Notification : String {
        case makingCoffee
        case coffeeMade
    }
}

这样就可以有一个统一的方式获取通知名称:

1
2
3
let coffeeMade = Barista.Notification.coffeeMade.rawValue
NSNotificationCenter.defaultCenter()
    .postNotificationName(coffeeMade)

避免通知名称冲突

我们可以为通知添加一个唯一的命名空间(namespace)来避免冲突。这里想到的解决方案是使用实现这个协议的object名称,因为每个object的名称在一个项目里是唯一的。

1
2
3
4
5
6
let baristaNotification =
    "\(Barista).\(Barista.Notification.coffeeMade.rawValue)"
let traineeNotification =
    "\(Trainee).\(Trainee.Notification.coffeeMade.rawValue)"
// baristaNotification: Barista.coffeeMade
// traineeNotification: Trainee.coffeeMade

但是每个通知都要手动添加就太蛋疼了。我们给这个协议加一个拓展方法来生成唯一的通知名称。因为这个方法只需要内部知道,所以标记为private。

1
2
3
4
5
public extension Notifier where Notification.RawValue == String {
    private static func nameFor(notification: Notification) -> String {
        return "\(self).\(notification.rawValue)"
    }
}

Notifier的扩展方法

添加观察者

最后一个通知的参数类型就是前面定义的那个枚举类型,这样就不用输入通知名称的字符串。

1
2
3
4
5
static func addObserver(observer: AnyObject, selector: Selector, notification: Notification) {
    let name = nameFor(notification)
    NSNotificationCenter.defaultCenter()
        .addObserver(observer, selector: selector, name: name, object: nil)
}

这样在使用的时候,在实现协议的object上直接方便的添加观察者:

1
Barista.addObserver(customer, selector: #selector(Customer.drink(_:)), notification: .coffeeMade)

发送通知

调用的时候应该是这样的:

1
Barista.postNotification(.coffeeMade)

这里利用了swfit的默认参数,object和userinfo设置一个默认的空值。实现如下:

1
2
3
4
5
static func postNotification(notification: Notification, object: AnyObject? = nil, userInfo: [String : AnyObject]? = nil) {
    let name = nameFor(notification)
    NSNotificationCenter.defaultCenter()
        .postNotificationName(name, object: object, userInfo: userInfo)
}

移除观察

这个实现就不贴了。和前面两个方法类似。调用的时候是这样的:

1
Barista.removeObserver(customer, notification: .coffeeMade)

总结

通 过灵活利用swift的语言特性:协议关联类型,协议可以添加默认的方法实现以及方法的默认参数,利用自定义的Notifier协议封装了 NSNotificationCenter的调用方式,解决了传统NSNotificationCenter调用的可能产生的三个潜在风险。

源码:https://github.com/andyyhope/Blog_NSNotificationCenterProtocol

posted @ 2016-07-14 17:04  橘子与布丁  阅读(217)  评论(0编辑  收藏  举报