66
77## Summary
88
9- Better Notification Observers for Swift.
9+ Better Notification & Key Value Observers for Swift.
1010
1111* Simpler API with sensible defaults
1212* Easier to avoid 'dangling' observers
@@ -23,7 +23,10 @@ it, simply add the following line to your Podfile:
2323pod ' HSObserver'
2424```
2525
26- ## Observers are Released
26+ Or Install as a swift package
27+
28+
29+ ## Observers are Released Automatically
2730
2831``` swift
2932class Watcher {
@@ -164,26 +167,45 @@ Post a notification directly
164167
165168 ``` swift
166169 class Watcher {
167- static let wave = NSNotification.Name .init (" waveNotification" )
170+ struct Notif {
171+ static let wave = NSNotification.Name .init (" waveNotification" )
172+ }
173+
168174
169175 func doPosting () {
170- Watcher.wave .post ()
176+ Watcher.Notif . wave .post ()
171177 // or
172- Watcher.wave .post (object :self ,userInfo :[" Foo" : " Bar" ])
178+ Watcher.Notif . wave .post (object :self ,userInfo :[" Foo" : " Bar" ])
173179 }
174180 }
175181 ```
176182
177183Assume the default notification centre and default options when posting directly from NotificationCenter
184+ (I strongly reccomend that you structure your notifications within a Notif struct of the relevant object. It makes things really easy to read)
178185
179186 ``` swift
180187
181- NotificationCenter.post (Watcher.wave )
188+ NotificationCenter.post (Watcher.Notif . wave )
182189 // is equivalent to
183190 NotificationCenter.default .post (Watcher.wave ,object :nil )
184191
185192 ```
186193
194+ ## Now with Key Value Notifications
195+
196+ for example, to observe the duration of an AVPlayerItem
197+
198+ ``` swift
199+ durationObserver = HSKeyPathObserver.init (forKeyPath : " duration" ,
200+ of : item,
201+ activate :true ) {
202+ [weak self ](_ ) in
203+ self ? .generateImages ()
204+ }
205+ ```
206+
207+ (again, remember to keep a reference to durationObserver or it will disappear)
208+
187209
188210## Author
189211
0 commit comments