ESNotification 0.3.0

ESNotification 0.3.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Apr 2016
SPMSupports SPM

Maintained by Tomohiro Kumagai.



 
Depends on:
Swim>= 1.4.1
ESThread>= 0.1.2
 

A type-safe notification management system for iOS/OSX written in Swift 2.

It is a swift module that powerfully notification management system using type-safe notification types. Notifications using this system are compatible with NSNotificationCenter.

This module written in Swift 2.0 and the module supports Objective-C.

Installation

ESNotification can install using CocoaPods.

pod 'ESNotification'

Usage in Swift

Definition

You define Notifications by Object that confirms to Notification Protocol.

final class MyNativeNotification : Notification {

}

You can also implement properties and methods in the object. It work user information of the notification. It can be accessed type-safely.

final class MyNativeNotification : Notification {

    var serial:Int
    var validation:Bool
}

Observe using NotificationObservable

Preparation

First, an object which observe some notifications need to conforms to NotificationObservable protocol.

class ViewController : NSViewController, NotificationObservable {

    var notificationHandlers = NotificationHandlers()

}

Native Notification

Then, to observe an Native Notification (e.g. MyNativeNotification), use observeNotification: method defined in NotificationObservable protocol.

self.observeNotification { [unowned self] (notification: MyNativeNotification) in

    ...
}

In other way, you can specify an notification type using argument too.

self.observeNotification(MyNativeNotification.self) { [unowned self] notification in

    ...
}

Named Notification (includes NSNotification)

You can observer a Named Notification (includes Legacy NSNotification) using observeNotificationNamed:handler method defined in NotificationObservable protocol.

self.observeNotificationNamed(NSApplicationWillTerminateNotification) { [unowned self] notification in

    ...
}

Type of notification argument is NamedNotification type.

Release Notification Handlers

If you want to all notification handlers which observe by self, you can release all notifications using releaseAllObservingNotifications method.

self.releaseAllObservingNotifications

When you want to release a Notification Handler implicitly, save HandlerID returns by observeNotification method, and call release method of the Handler ID.

let handleID = self.observeNotificationNamed(NSApplicationWillTerminateNotification) { [unowned self] notification in

    ...
}
handleID.release()

Observe using Notification Type

Native Notification

You can observe a Notification using observeBy:handler: method privided by a type conforms to Notification protocol.

let handlerID = MyNativeNotification.observe { [unowned self] notification -> Void in

    ...
}

Usually, you want to use self instance in handler closure, you need to pass the instance as an unowned reference or a weak reference using capture list.

Named Notification (includes NSNotification)

You can observe a Named Notification easily by using observe:by:handler: method. If same name of named notification posted, you can handle it.

let handlerID = NamedNotification.observe(name) { [unowned self] notification in

    ...
}

NSNotification can be handle in the same way.

Release Notification Handlers

You invoke observe method in Notification Type, you must release the Handler manually.

handleID.release()

Always Handling on Main Thread

When a notification of Notification Type posted, the handler closure is called on main thread.

At this time, the posted notification passed to parameter of handler function. The notification can use as Handled Notification type own.

Post a Notification

You can post a Notification using post function.

MyNativeNotification().post()

You can post a Named Notification too.

NamedNotification(NSApplicationWillTerminateNotification).post()

You can post a Named Notification using Legacy NSNotificationCenter too. The following code is same to below code.

let notification = NSNotification(name: NSApplicationWillTerminateNotification, object: nil)

NSNotificationCenter.defaultCenter().postNotification(notification)

Usage in Objective-C

Define Native Notifications

In Swift, if you want to post a Native Notification in Objective-C, You must your notification class conforms to Notification protocol and export to Objective-C by inheriting NSObject class.

class MyNativeNotification : NSObject, Notification {
}

In Objective-C, you must your notification class conforms to ESNotification protocol.

@interface MyNativeNotification : NSObject <ESNotification>

@end

Observing

You can observe Native Notification in Objective-C. To observing a Native Notification, you use - addObserver:selector:ESNotification:object; method implemented in NSNotificationCenter.

NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];

[nc addObserver:self selector:@selector(myNativeNotificationReceived:) ESNotification:[MyNativeNotification class] object:nil];

When a Native notification received by NSNotificationCenter, the Native Notification instance set to object property of NSNotification passed by parameter.

- (void)myNativeNotificationReceived:(NSNotification*)note
{
    MyNativeNotification* nativeNotification = note.object;

}

Post a Native Notification

You call the method when you want to post a Native Notification.

NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
MyNativeNotification* notification = [[MyNativeNotification alloc] init];

[notificationCenter postESNotification:notification];