| 测试已测试 | ✗ |
| 语言语言 | SwiftSwift |
| 许可协议 | MIT |
| 发布最后发布 | 2016年4月 |
| SPM支持SPM | ✗ |
由Tomohiro Kumagai维护。
| 依赖项 | |
| Swim | >= 1.4.1 |
| ESThread | >= 0.1.2 |
Swift 2编写的iOS/OSX类型安全通知管理系统。
这是一个使用类型安全通知类型的强大通知管理系统模块。使用该系统的通知与NSNotificationCenter兼容。
此模块使用Swift 2.0编写,并且该模块支持Objective-C。
ESNotification可以通过CocoaPods进行安装。
pod 'ESNotification'
您可以通过符合Notification协议的对象定义通知。
final class MyNativeNotification : Notification {
}您还可以在对象中实现属性和方法。它工作通知的用户信息。它可以通过类型安全访问。
final class MyNativeNotification : Notification {
var serial:Int
var validation:Bool
}首先,一个需要观察某些通知的对象需要符合NotificationObservable协议。
class ViewController : NSViewController, NotificationObservable {
var notificationHandlers = NotificationHandlers()
}然后,为了观察本地通知(例如,MyNativeNotification),请使用在NotificationObservable协议中定义的observeNotification:方法。
self.observeNotification { [unowned self] (notification: MyNativeNotification) in
...
}另一种方式,您还可以使用参数指定一个通知类型。
self.observeNotification(MyNativeNotification.self) { [unowned self] notification in
...
}您可以使用在NotificationObservable协议中定义的observeNotificationNamed:handler方法来观察命名通知(包括Legacy NSNotification)。
self.observeNotificationNamed(NSApplicationWillTerminateNotification) { [unowned self] notification in
...
}
notification参数的类型是NamedNotification类型。
如果您想释放通过self观察的所有通知处理程序,您可以使用releaseAllObservingNotifications方法释放所有通知。
self.releaseAllObservingNotifications当您想隐式释放一个Notification Handler时,保存由observeNotification方法返回的HandlerID,并调用该Handler ID的release方法。
let handleID = self.observeNotificationNamed(NSApplicationWillTerminateNotification) { [unowned self] notification in
...
}handleID.release()您可以使用符合Notification协议的类型提供的observeBy:handler:方法来观察通知。
let handlerID = MyNativeNotification.observe { [unowned self] notification -> Void in
...
}通常,您在
handler闭包中使用self实例,需要将实例作为非拥有的引用或使用捕获列表的弱引用传递。
您可以通过使用observe:by:handler:方法轻松观察命名通知。如果发布了相同名称的命名通知,您可以处理它。
let handlerID = NamedNotification.observe(name) { [unowned self] notification in
...
}NSNotification也可以以同样的方式处理。
您在通知类型中调用observe方法时,必须手动释放处理程序。
handleID.release()当发布类型的Notification通知时,handler闭包在主线程上被调用。
此时,发布的通知被传递给了handler函数的参数。您可以像使用自己的处理通知类型一样使用此通知。
您可以使用post函数发布通知。
MyNativeNotification().post()您还可以发布命名通知。
NamedNotification(NSApplicationWillTerminateNotification).post()您还可以使用遗留的NSNotificationCenter发布命名通知。下面的代码与下面的代码相同。
let notification = NSNotification(name: NSApplicationWillTerminateNotification, object: nil)
NSNotificationCenter.defaultCenter().postNotification(notification)在Swift中,如果您想在Objective-C中发布本地通知,则必须使您的通知类符合Notification协议,并通过继承NSObject类将其导出到Objective-C。
class MyNativeNotification : NSObject, Notification {
}在Objective-C中,您必须使您的通知类符合ESNotification协议。
@interface MyNativeNotification : NSObject <ESNotification>
@end您可以在Objective-C中观察本地通知。为了观察一个本地通知,您使用在NSNotificationCenter中实现的- addObserver:selector:ESNotification:object;方法。
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(myNativeNotificationReceived:) ESNotification:[MyNativeNotification class] object:nil];当NSNotification收到本地通知时,本地通知实例被设置为由参数传递的NSNotification的object属性。
- (void)myNativeNotificationReceived:(NSNotification*)note
{
MyNativeNotification* nativeNotification = note.object;
}当您想要发布一个本地通知时,调用此方法。
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
MyNativeNotification* notification = [[MyNativeNotification alloc] init];
[notificationCenter postESNotification:notification];