Skip to content

Instantly share code, notes, and snippets.

@cbess
Last active August 10, 2019 15:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbess/b62c1aca213c058221e0 to your computer and use it in GitHub Desktop.
Save cbess/b62c1aca213c058221e0 to your computer and use it in GitHub Desktop.
Simple NSNotificationCenter wrapper for Swift 4.x
//
// AppNotify.swift
//
// Created by Christopher Bess on 6/30/15.
// MIT License
//
import Foundation
/// Represents the app notification.
class AppNotify {
private static var notificationCenter: NotificationCenter {
return NotificationCenter.default
}
/// Post the specified notification with the given object
static func postNotification(_ name: String, object: Any? = nil) {
notificationCenter.post(name: NSNotification.Name(rawValue: name), object: object)
}
/// Post the specified NSNotification with the given object
static func postNotification(_ name: NSNotification.Name, object: Any? = nil) {
notificationCenter.post(name: name, object: object)
}
/// Observe the notification with the specified name
static func observeNotification(_ observer: Any, selector: Selector, name: String, object: Any? = nil) {
notificationCenter.addObserver(observer, selector: selector, name: NSNotification.Name(rawValue: name), object: object)
}
/// Observe the NSNotification with the specified name
static func observeNotification(_ observer: Any, selector: Selector, name: NSNotification.Name, object: Any? = nil) {
notificationCenter.addObserver(observer, selector: selector, name: name, object: object)
}
/// Remove the object from the observers
static func removeObserver(_ object: Any) {
notificationCenter.removeObserver(object)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment