Latest 1.1
Homepage https://github.com/mergesort/TypedNotifications
License MIT
Platforms ios 8.0, requires ARC
Frameworks Foundation
Authors

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

You can register notifications for either payload containing notifications, or payload-free notifications.

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

Sending Notifications

You can send notifications for either payload containing notifications, or payload-free notifications.

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

Extracting values from Notifications

Only payload containing notifications can have their payload extracted, because, duh.

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

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

If you have no payload and just want to send a message, use a TypedNotification like so.

struct SomeEventNotification: TypedNotification {}

For our example, let’s use a TypedPayloadNotification with a payload though.

struct TypedPersonNotification: TypedPayloadNotification {

    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 GenericTypedPayloadNotification<T>: TypedPayloadNotification {

    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.

Latest podspec

{
    "name": "TypedNotifications",
    "version": "1.1",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "homepage": "https://github.com/mergesort/TypedNotifications",
    "authors": {
        "Joe Fabisevich": "[email protected]"
    },
    "summary": "A wrapper around NotificationCenter for sending typed notifications with payloads across your iOS app.",
    "source": {
        "git": "https://github.com/mergesort/TypedNotifications.git",
        "tag": "1.1"
    },
    "source_files": "Source/*.swift",
    "frameworks": "Foundation",
    "requires_arc": true,
    "social_media_url": "https://twitter.com/mergesort",
    "platforms": {
        "ios": "8.0"
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This