Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Notification with enum.
import Foundation
import UIKit
enum NotificationKey: String {
case Hoge
}
//--------------- pattern1: use protocol-extension
protocol NSNotificationObservable: RawRepresentable {
var rawValue: String { get }
}
extension NSNotificationObservable {
func addObserver(observer: AnyObject, selector: Selector, object: AnyObject? = nil) {
NSNotificationCenter.defaultCenter().addObserver(observer, selector: selector, name: self.rawValue, object: object)
}
func addObserver(object: AnyObject? = nil, queue: NSOperationQueue? = nil, usingBlock block: NSNotification -> Void) -> NSObjectProtocol {
return NSNotificationCenter.defaultCenter().addObserverForName(self.rawValue, object: object, queue: queue, usingBlock: block)
}
func post(object: AnyObject? = nil, userInfo: [NSObject: AnyObject]? = nil) {
NSNotificationCenter.defaultCenter().postNotificationName(self.rawValue, object: object, userInfo: userInfo)
}
func removeObserver(observer: AnyObject, object: AnyObject? = nil) {
NSNotificationCenter.defaultCenter().removeObserver(observer, name: self.rawValue, object: object)
}
}
// adapt NotificationObservable to NotificationKey enum
extension NotificationKey: NSNotificationObservable {}
//-------------- pattern2 : extend NSNotificationCenter for enum key
public extension NSNotificationCenter {
func addObserver<Key: RawRepresentable where Key.RawValue == String>(observer: AnyObject, selector aSelector: Selector, key: Key?, object anObject: AnyObject?) {
self.addObserver(observer, selector: aSelector, name: key?.rawValue, object: anObject)
}
func postNotificationKey<Key: RawRepresentable where Key.RawValue == String>(key: Key, object anObject: AnyObject?) {
self.postNotificationName(key.rawValue, object: anObject)
}
func postNotificationKey<Key: RawRepresentable where Key.RawValue == String>(key: Key, object anObject: AnyObject?, userInfo aUserInfo: [NSObject : AnyObject]?) {
self.postNotificationName(key.rawValue, object: anObject, userInfo: aUserInfo)
}
func removeObserver<Key: RawRepresentable where Key.RawValue == String>(observer: AnyObject, key: Key?, object anObject: AnyObject?) {
self.removeObserver(observer, name: key?.rawValue, object: anObject)
}
func addObserverForKey<Key: RawRepresentable where Key.RawValue == String>(key: Key?, object obj: AnyObject?, queue: NSOperationQueue?, usingBlock block: (NSNotification) -> Void) -> NSObjectProtocol {
return self.addObserverForName(key?.rawValue, object: obj, queue: queue, usingBlock: block)
}
}
class AAA {
@objc func hoge() {
print("!!!!!")
}
}
let anObject = AAA()
// example: pattern1
NotificationKey.Hoge.addObserver(anObject, selector: #selector(anObject.hoge))
NotificationKey.Hoge.post()
NotificationKey.Hoge.removeObserver(anObject)
// example: pattern2
NSNotificationCenter.defaultCenter().addObserver(anObject, selector: #selector(anObject.hoge), key: NotificationKey.Hoge, object: nil)
NSNotificationCenter.defaultCenter().postNotificationKey(NotificationKey.Hoge, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(anObject, key: NotificationKey.Hoge, object: nil)
// ---- normal usage
NSNotificationCenter.defaultCenter().addObserver(anObject, selector: #selector(anObject.hoge), name: NotificationKey.Hoge.rawValue, object: nil)
NSNotificationCenter.defaultCenter().postNotificationName(NotificationKey.rawValue, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(anObject, name: NotificationKey.Hoge.rawValue, object: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment