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
priorityis not yet implementedDeclaration
Swift
public func addObserver(forEvent event: Event.Name, name: String? = nil, priority: UInt8 = ObserverPriority.normal, callback: @escaping ObserverCallback)Parameters
eventEvent.Name(a.k.a.Notification.Name) the observer must be registered tonameOptional name of the oberserver, can be used to remove the observer later
prioritythe priority of the observer
callbackthe 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
eventevent 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
namename of the observer(s) to be removed
eventevent 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&userInfoUsage 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
eventname of the event
objectsingle object you want to pass to the observer
userInfodictionnary you want to pass to the observer
EventCenter Class Reference