Created
August 12, 2016 11:38
-
-
Save ppeszko/a25627afd11ae8c0085f17e0120c6624 to your computer and use it in GitHub Desktop.
Example code for https://medium.com/@PatrykPeszko/notification-pattern-a862ceff4226
This file contains hidden or 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
| // 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