typ

TypedNotifications

A mechanism for sending typed notifications with payloads across your iOS app.

Showing:

Popularity

Downloads/wk

0

GitHub Stars

69

Maintenance

Last Commit

2yrs ago

Contributors

3

Package

Dependencies

0

License

MIT

Categories

Readme

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

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

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

Sending Notifications

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

func post<T: TypedNotification>(typedNotification: T, object: Any? = nil)
func post<T: TypedPayloadNotification>(typedNotification: T, object: Any? = nil)

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

SPM will be the default supported installation method from version 1.4.0 and higher, so please integrate by using SPM.

If you're still using CocoaPods for version 1.4.0 or below you can 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.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial