Giter Club home page Giter Club logo

ritypednotification's Introduction

TypedNotification

The TypedNotification microframework allows you to use NotificationCenter to post and receive strongly-typed notifications with associated data, without having to manually pack and unpack a userInfo dictionary.

Using a typed notification is simple. First, define a notification type:

struct MyNotification: TypedNotification {
    var value: String
}

Next, use NotificationCenter to add an observer for that notification:

class MyViewController: UIViewController {
    private var token: NotificationToken?

    override func viewDidLoad() {
        super.viewDidLoad()

        token = NotificationCenter.default.addObserver(for: MyNotification.self) { [weak self] (notification) in
            self?.received(notification)
        }
    }

    private func received(_ notification: MyNotification) {
        print(notification.value)
    }
}

In the example, MyViewController retains the NotificationToken returned by NotificationCenter.addObserver(). When the MyViewController instance is deallocated, the NotificationToken is deallocated and the observer is deregistered. The [weak self] capture list is important. Without it, there will be a retain cycle causing a memory leak.

Finally, use NotificationCenter to post notifications:

NotificationCenter.default.post(MyNotification(value: "foobar"), from: self)

ritypednotification's People

Contributors

pwc3 avatar tylerkirtland avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.