Skip to content

Instantly share code, notes, and snippets.

@brocoo
Last active May 13, 2016 09:51
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brocoo/19481abd4e0ec25f6257 to your computer and use it in GitHub Desktop.
Save brocoo/19481abd4e0ec25f6257 to your computer and use it in GitHub Desktop.
Extension of NSNotification to use a key enum as the notification name.
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)
}
}
@Dylanooo
Copy link

But how about UIKeyboardWillHideNotification?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment