|
|
# NotificationCenter
|
|
|
|
|
|
## Notification
|
|
|
|
|
|
```swift
|
|
|
protocol Notification {
|
|
|
associatedtype Object: Equatable = NSNull
|
|
|
static var observers: [NotificationObserver<Self>] { get set }
|
|
|
}
|
|
|
```
|
|
|
|
|
|
```swift
|
|
|
Notification.observe(object: Object? = nil, handler: ((Self) -> Void)) -> NotificationObserver<Self>
|
|
|
|
|
|
Notification.post(object: Object? = nil)
|
|
|
```
|
|
|
|
|
|
## Example
|
|
|
|
|
|
```swift
|
|
|
struct EventNotification: Notification {
|
|
|
typealias Object = String
|
|
|
static var observers: [NotificationObserver<EventNotification>] = []
|
|
|
var eventDescription: String
|
|
|
}
|
|
|
|
|
|
EventNotification.observe(object: "EventA") { notification in
|
|
|
print(notification.eventDescription)
|
|
|
}
|
|
|
|
|
|
EventNotification(eventDescription: "Event A description").post(object: "EventA")
|
|
|
``` |