https://github.com/sobri909/swiftnotes
A super simple wrapper around NotificationCenter
https://github.com/sobri909/swiftnotes
events notificationcenter
Last synced: 6 months ago
JSON representation
A super simple wrapper around NotificationCenter
- Host: GitHub
- URL: https://github.com/sobri909/swiftnotes
- Owner: sobri909
- License: mit
- Created: 2017-11-26T11:00:16.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-10-15T08:31:43.000Z (about 5 years ago)
- Last Synced: 2025-06-16T19:56:21.504Z (7 months ago)
- Topics: events, notificationcenter
- Language: Swift
- Homepage:
- Size: 12.7 KB
- Stars: 42
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwiftNotes
A super simple wrapper around [NotificationCenter](https://developer.apple.com/documentation/foundation/nsnotificationcenter).
## Setup
```ruby
pod 'SwiftNotes'
```
Or just drop [SwiftNotes.swift](https://github.com/sobri909/SwiftNotes/blob/master/SwiftNotes/SwiftNotes.swift) into your project.
## UIKit Notification Examples
#### UIKeyboard Notifications
```swift
when(.UIKeyboardDidShow) { note in
// do stuff
}
```
#### UIApplication Notifications
```swift
when(.UIApplicationDidBecomeActive) { _ in
// do stuff
}
```
## Custom Notifications
#### Define A Custom Notification
```swift
// define the custom event name
extension NSNotification.Name {
static let somethingHappened = Notification.Name("somethingHappened")
}
```
#### Trigger Your Custom Notification
```swift
// send your custom event
trigger(.somethingHappened)
```
#### Respond To Your Custom Notification
```swift
when(.somethingHappened) { _ in
// do stuff
}
```
## Extra Parameters
#### Trigger An Event And Include UserInfo
```swift
trigger(.somethingHappened, userInfo: ["goodTimes": true])
```
#### Trigger An Event On A Specific Sender
```swift
trigger(.updatedFromRemote, on: self)
````
#### Observe An Event On A Specific Sender
```swift
when(model, does: .updatedFromRemote) { _ in
// do stuff
}
```
#### Respond On A Specific Queue
```swift
// make sure the closure is run on the main queue
when(.somethingHappened, doOn: OperationQueue.main) _ in
// do stuff
}
```