Created
August 15, 2016 18:29
-
-
Save cemolcay/54c26d4a02cd6f8580dccc8b2796f39f to your computer and use it in GitHub Desktop.
NSNotificationCenter wrapper in swift
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
| import Foundation | |
| enum KVOManagerNotificationType: String { | |
| case StickerBought = "com.mojilala.Stickers.Keyboard.StickerBought" | |
| } | |
| class KVOManager: NSObject { | |
| static let sharedManager = KVOManager() | |
| func addObserver( | |
| toObject object: NSObject, | |
| forNotificationType notificationType: KVOManagerNotificationType, | |
| withActionHandler actionHandler: (NSNotification) -> Void) { | |
| NSNotificationCenter.defaultCenter().addObserverForName( | |
| notificationType.rawValue, | |
| object: nil, | |
| queue: nil, | |
| usingBlock: actionHandler) | |
| } | |
| func removeObserver(fromObject object: NSObject, forNotificationType notificationType: KVOManagerNotificationType) { | |
| NSNotificationCenter.defaultCenter().removeObserver( | |
| object, | |
| name: notificationType.rawValue, | |
| object: nil) | |
| } | |
| func postNotification(forNotificationType notificationType: KVOManagerNotificationType) { | |
| NSNotificationCenter.defaultCenter().postNotificationName( | |
| notificationType.rawValue, | |
| object: nil) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment