Last active
August 10, 2019 15:58
-
-
Save cbess/b62c1aca213c058221e0 to your computer and use it in GitHub Desktop.
Simple NSNotificationCenter wrapper for Swift 4.x
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
// | |
// 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