TypedNotifications

A wrapper around NotificationCenter for sending typed notifications with payloads across your iOS app.


Keywords
asynchronous, ios, notifications, swift
License
MIT
Install
pod try TypedNotifications

Documentation

TypedNotifications

A wrapper around NotificationCenter for sending typed notifications with payloads across your iOS app.


BuddyBuild Pod Version Swift Version License MIT Plaform

Asynchronous behavior is hard. Let's make it a little easier so we can turn that frown (🙁) upside down (🙃).


Using TypedNotifications is easy. You can drop it into your app and replace all of your un-typed Notifications in minutes.


Registering for Notifications

func register<T>(type: T.Type, observer: Any, selector: Selector) where T: TypedNotification

Sending Notifications

func post<T>(typedNotification: T) where T: TypedNotification

Extracting values from Notifications

func getPayload<T>(notificationType: T.Type) -> T.Payload? where T: TypedNotification

Now that might look a little scary at first with all those Ts, but let's break it down with some examples and show you how easy this is.

Examples

Create some values you'd like to send through your app.

enum Job {
    
    case softwareDeveloper
    case designer
    case conArtist

}

struct Person {

    let name: String
    let job: Job

}

Create the notification to send your value

struct TypedPersonNotification: TypedNotification {

    let payload: Person

}

Register the notification

NotificationCenter.default.register(type: TypedPersonNotification.self, observer: self, selector: #selector(personNotificationWasReceived))

Send the notification

let amanda = Person(name: "Amanda", job: .softwareDeveloper)
let amandaNotification = TypedPersonNotification(payload: amanda)
NotificationCenter.default.post(typedNotification: amandaNotification)

And handle the notification

@objc func personNotificationWasReceived(notification: Notification) {
    guard let person = notification.getPayload(notificationType: TypedPersonNotification.self) else {
        os_log("Could not properly retrieve payload from TypedPersonNotification")
        return
    }
    
    let nameText = "Name: \(person.name)"
    let jobText = "Job: \(person.job.title)"

    print("Got our Person payload!\n\(nameText)\n\(jobText)")
}

And that's it! You've sent a typed notification throughout your app.

If you want to play on expert mode, I recommend using generics and passing notifications through your app that way.

struct GenericTypedNotification<T>: TypedNotification {

    let payload: T

}

Requirements

  • iOS 8.0+
  • Xcode 7.3+

Installation

You can use CocoaPods to install TypedNotifications by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!

pod 'TypedNotifications'

Or install it manually by downloading TypedNotifications.swift and dropping it in your project.

About me

Hi, I'm Joe everywhere on the web, but especially on Twitter.

License

See the license for more information about how you can use TypedNotifications.