Skip to content

Commit e3e5be5

Browse files
Tidy up docs
1 parent 3143246 commit e3e5be5

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/HSObserver.xcscheme

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
3030
<Testables>
31+
<TestableReference
32+
skipped = "NO">
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "HSObserverTests"
36+
BuildableName = "HSObserverTests"
37+
BlueprintName = "HSObserverTests"
38+
ReferencedContainer = "container:">
39+
</BuildableReference>
40+
</TestableReference>
3141
</Testables>
3242
</TestAction>
3343
<LaunchAction

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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:
2323
pod 'HSObserver'
2424
```
2525

26-
## Observers are Released
26+
Or Install as a swift package
27+
28+
29+
## Observers are Released Automatically
2730

2831
```swift
2932
class 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

177183
Assume 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

Sources/HSObserver/HSKeyPathObserver.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public class HSKeyPathObserver:NSObject, HSObserves {
1515
private var options:NSKeyValueObservingOptions
1616
weak private var object:AnyObject?
1717

18+
19+
/// Based on addObserver(_:forKeyPath:options:context:), but with a callback block
20+
/// - Parameters:
21+
/// - keyPath: the string key path to observe
22+
/// - object: the object you want to observe
23+
/// - options: NSKeyValueObservingOptions
24+
/// - activate: whether to immediately activate the observer
25+
/// - block: the block to call when there is a change
1826
public init(forKeyPath keyPath: String,of object: AnyObject, options: NSKeyValueObservingOptions = [],activate:Bool = false, block:@escaping ([NSKeyValueChangeKey:Any]?)->Void ){
1927
self.block = block
2028
self.keyPath = keyPath

0 commit comments

Comments
 (0)