Skip to content

Instantly share code, notes, and snippets.

@ppeszko
Created August 12, 2016 11:38
Show Gist options
  • Select an option

  • Save ppeszko/a25627afd11ae8c0085f17e0120c6624 to your computer and use it in GitHub Desktop.

Select an option

Save ppeszko/a25627afd11ae8c0085f17e0120c6624 to your computer and use it in GitHub Desktop.
// In the past I was calling it Notification but iOS10
// NSNotification was renamed to Notification, so ¯\_(ツ)_/¯
protocol Notifiable: RawRepresentable {
func post(userInfo aUserInfo: [NSObject : AnyObject]?, object anObject: AnyObject?)
func observe(observer: AnyObject, selector aSelector: Selector, object anObject: AnyObject?)
func name() -> String
func notificationCenter() -> NSNotificationCenter
}
extension Notifiable {
func post(userInfo aUserInfo: [NSObject : AnyObject]? = nil, object anObject: AnyObject? = nil) {
notificationCenter().postNotificationName(self.name(),
object: anObject,
userInfo: aUserInfo)
}
func observe(observer: AnyObject, selector aSelector: Selector, object anObject: AnyObject? = nil) {
notificationCenter().addObserver(observer,
selector: aSelector,
name: name(),
object: anObject)
}
func name() -> String {
return "\(self.dynamicType).\(self.rawValue)"
}
func notificationCenter() -> NSNotificationCenter {
return NSNotificationCenter.defaultCenter()
}
var description: String {
return name()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment