Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created August 15, 2016 18:29
Show Gist options
  • Select an option

  • Save cemolcay/54c26d4a02cd6f8580dccc8b2796f39f to your computer and use it in GitHub Desktop.

Select an option

Save cemolcay/54c26d4a02cd6f8580dccc8b2796f39f to your computer and use it in GitHub Desktop.
NSNotificationCenter wrapper in swift
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