Skip to content

Commit

Permalink
Tidy up docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ConfusedVorlon committed Jul 30, 2020
1 parent 3143246 commit e3e5be5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
10 changes: 10 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/HSObserver.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "HSObserverTests"
BuildableName = "HSObserverTests"
BlueprintName = "HSObserverTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Summary

Better Notification Observers for Swift.
Better Notification & Key Value Observers for Swift.

* Simpler API with sensible defaults
* Easier to avoid 'dangling' observers
Expand All @@ -23,7 +23,10 @@ it, simply add the following line to your Podfile:
pod 'HSObserver'
```

## Observers are Released
Or Install as a swift package


## Observers are Released Automatically

```swift
class Watcher {
Expand Down Expand Up @@ -164,26 +167,45 @@ Post a notification directly

```swift
class Watcher {
static let wave = NSNotification.Name.init("waveNotification")
struct Notif {
static let wave = NSNotification.Name.init("waveNotification")
}


func doPosting() {
Watcher.wave.post()
Watcher.Notif.wave.post()
//or
Watcher.wave.post(object:self,userInfo:["Foo":"Bar"])
Watcher.Notif.wave.post(object:self,userInfo:["Foo":"Bar"])
}
}
```

Assume the default notification centre and default options when posting directly from NotificationCenter
(I strongly reccomend that you structure your notifications within a Notif struct of the relevant object. It makes things really easy to read)

```swift

NotificationCenter.post(Watcher.wave)
NotificationCenter.post(Watcher.Notif.wave)
//is equivalent to
NotificationCenter.default.post(Watcher.wave,object:nil)

```

## Now with Key Value Notifications

for example, to observe the duration of an AVPlayerItem

```swift
durationObserver = HSKeyPathObserver.init(forKeyPath: "duration",
of: item,
activate:true) {
[weak self](_) in
self?.generateImages()
}
```

(again, remember to keep a reference to durationObserver or it will disappear)


## Author

Expand Down
8 changes: 8 additions & 0 deletions Sources/HSObserver/HSKeyPathObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public class HSKeyPathObserver:NSObject, HSObserves {
private var options:NSKeyValueObservingOptions
weak private var object:AnyObject?


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

0 comments on commit e3e5be5

Please sign in to comment.