Last active
May 13, 2016 09:51
-
-
Save brocoo/19481abd4e0ec25f6257 to your computer and use it in GitHub Desktop.
Extension of NSNotification to use a key enum as the notification name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum NotificationKey: String { | |
case UserSignedIn = "UserSignedInNotification" | |
case UserSignedOut = "UserSignedOutNotification" | |
case SomeOtherEvent = "SomeOtherEventNotification" | |
} | |
extension NSNotificationCenter { | |
func addObserver(observer: AnyObject, selector aSelector: Selector, key aKey: NotificationKey) { | |
self.addObserver(observer, selector: aSelector, name: aKey.rawValue, object: nil) | |
} | |
func addObserver(observer: AnyObject, selector aSelector: Selector, key aKey: NotificationKey, object anObject: AnyObject?) { | |
self.addObserver(observer, selector: aSelector, name: aKey.rawValue, object: anObject) | |
} | |
func removeObserver(observer: AnyObject, key aKey: NotificationKey, object anObject: AnyObject?) { | |
self.removeObserver(observer, name: aKey.rawValue, object: anObject) | |
} | |
func postNotificationKey(key: NotificationKey, object anObject: AnyObject?) { | |
self.postNotificationName(key.rawValue, object: anObject) | |
} | |
func postNotificationKey(key: NotificationKey, object anObject: AnyObject?, userInfo aUserInfo: [NSObject : AnyObject]?) { | |
self.postNotificationName(key.rawValue, object: anObject, userInfo: aUserInfo) | |
} | |
func addObserverForKey(key: NotificationKey, object obj: AnyObject?, queue: NSOperationQueue?, usingBlock block: (NSNotification!) -> Void) -> NSObjectProtocol { | |
return self.addObserverForName(key.rawValue, object: obj, queue: queue, usingBlock: block) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But how about UIKeyboardWillHideNotification?