Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 35 additions & 44 deletions Blog_NSNotificationCenter.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,80 +1,71 @@
import Foundation

// MARK: - Protocol

// MARK: - Notifier Protocol
public protocol Notifier {

associatedtype Notification: RawRepresentable

}

// MARK: - Notifier base implementation
public extension Notifier where Notification.RawValue == String {

// MARK: - Static Computed Variables

private static func nameFor(notification: Notification) -> String {
return "\(self).\(notification.rawValue)"
}


// MARK: - Instance Methods

// Pot

func postNotification(notification: Notification, object: AnyObject? = nil) {
Self.postNotification(notification, object: object)
// MARK: Calculated notification name
private static func notificationName(for notification: Notification) -> NSNotification.Name {
return NSNotification.Name(rawValue: "\(self).\(notification.rawValue)")
}

func postNotification(notification: Notification, object: AnyObject? = nil, userInfo: [String : AnyObject]? = nil) {
Self.postNotification(notification, object: object, userInfo: userInfo)
}


// MARK: - Static Function

// Post

static func postNotification(notification: Notification, object: AnyObject? = nil, userInfo: [String : AnyObject]? = nil) {
let name = nameFor(notification)
// MARK: Add observer
static func addObserver(_ observer: AnyObject, selector: Selector, notification: Notification) {
let name = notificationName(for: notification)

NSNotificationCenter.defaultCenter()
.postNotificationName(name, object: object, userInfo: userInfo)
NotificationCenter.default.addObserver(observer, selector: selector, name: name, object: nil)
}

// Add

static func addObserver(observer: AnyObject, selector: Selector, notification: Notification) {
let name = nameFor(notification)
// MARK: Post notification
static func postNotification(_ notification: Notification, object: AnyObject? = nil, userInfo: [String : AnyObject]? = nil) {
let name = notificationName(for: notification)

NSNotificationCenter.defaultCenter()
.addObserver(observer, selector: selector, name: name, object: nil)
NotificationCenter.default.post(name: name, object: object, userInfo: userInfo)
}

// Remove
func postNotification(_ notification: Notification, object: AnyObject? = nil, userInfo: [String : AnyObject]? = nil) {
Self.postNotification(notification, object: object, userInfo: userInfo)
}

static func removeObserver(observer: AnyObject, notification: Notification, object: AnyObject? = nil) {
let name = nameFor(notification)

NSNotificationCenter.defaultCenter()
.removeObserver(observer, name: name, object: object)
// MARK: Remove observer
static func removeObserver(_ observer: Any, notification: Notification? = nil) {
if let notification = notification {
NotificationCenter.default.removeObserver(observer, name: notificationName(for: notification), object: nil)
} else {
NotificationCenter.default.removeObserver(observer)
}
}

}


// MARK: - Example

class Barista : Notifier {
enum Notification : String {
class Barista: Notifier {
enum Notification: String {
case makeCoffee
}

}

extension Selector {

static let makeCoffeeNotification = #selector(Customer.drink(_:))

}

class Customer {
@objc func drink(notification: NSNotification) {

@objc func drink(_ notification: NSNotification) {
print("Mmm... Coffee")
}

}


Expand Down