From 1b78658906aacfa4a766b3d6b6fc32d7ecbdb57d Mon Sep 17 00:00:00 2001 From: Mattijah Date: Wed, 6 Sep 2017 14:15:43 +0100 Subject: [PATCH] Add an ability to copy the value from Observable without having to access it first --- Observable-Swift/Protocols.swift | 7 ++++++- README.md | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Observable-Swift/Protocols.swift b/Observable-Swift/Protocols.swift index 77264cf..0bd0b36 100644 --- a/Observable-Swift/Protocols.swift +++ b/Observable-Swift/Protocols.swift @@ -71,7 +71,7 @@ public protocol OwnableObservable: AnyObservable { } -// observable <- value +// (observable <- value) or (observable.value <- observable.value) infix operator <- // value = observable^ @@ -117,6 +117,11 @@ public func += (event: EventReference>, handler: @escaping (T return event.add({ handler($0.newValue) }) } +// for observable values on observable values +public func <- (x: inout T, y: T) { + x.value = y.value +} + // for observable values on variables public func <- (x: inout T, y: T.ValueType) { x.value = y diff --git a/README.md b/README.md index 6d3ce11..3220bd1 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,12 @@ x.afterChange += { println("Changed x from \($0) to \($1)") } // change the value, prints "Changed x from 0 to 42" x <- 42 // alternativelyL x ^= 42, without operators: x.value = 42 + + +var y = Observable(0) + +// Copy the value from one observable to another one, prints "Changed x from 42 to 0" +x <- y ``` You can, of course, have observable properties in a `class` or a `struct`: