EventCenter

public class EventCenter

EventCenter class to add & remove observers for specific events.

Usage Example:

import EventCenter
  • Initialize Event Center.

    Usage Example:

    let ec = EventCenter()
    

    Declaration

    Swift

    public init()
  • Add observer with optional name for specific event.

    Usage Example:

    ec.addObserver(forEvent: Event.Name("event1"), name: "obs1", callback: { event: Event in
        if let obj = event.object {
            print("Hello, \(obj)!")
        }
    })
    

    Warning

    priority is not yet implemented

    Declaration

    Swift

    public func addObserver(forEvent event: Event.Name, name: String? = nil, priority: UInt8 = ObserverPriority.normal, callback: @escaping ObserverCallback)

    Parameters

    event

    Event.Name (a.k.a. Notification.Name) the observer must be registered to

    name

    Optional name of the oberserver, can be used to remove the observer later

    priority

    the priority of the observer

    callback

    the function you want to perform when the event is posted

  • Remove all observers of specific event.

    Usage Example:

    ec.removeObservers(forEvent: Event.Name("event1"))
    

    Declaration

    Swift

    public func removeObservers(forEvent event: Event.Name)

    Parameters

    event

    event name

  • Remove all observers of specific event.

    Usage Example:

    ec.removeObserver(name: "obs1", forEvent: Event.Name("event1"))
    

    Declaration

    Swift

    public func removeObserver(name: String?, forEvent event: Event.Name)

    Parameters

    name

    name of the observer(s) to be removed

    event

    event name

  • Remove all the observers from the Event Center.

    Usage Example:

    ec.removeAllObservers()
    

    Declaration

    Swift

    public func removeAllObservers()
  • Post an event with an optional object & userInfo

    Usage Example:

    ec.post(event: Event.Name("event1"), object: "World", userInfo: [1: "String", "id": 42])
    

    Declaration

    Swift

    public func post(event: Event.Name, object: Any? = nil, userInfo: [AnyHashable: Any]? = nil)

    Parameters

    event

    name of the event

    object

    single object you want to pass to the observer

    userInfo

    dictionnary you want to pass to the observer