From fc3ad86b6b9a5e90072e27398231d07c20c886fb Mon Sep 17 00:00:00 2001 From: Dieter Baron Date: Thu, 26 May 2016 01:04:11 +0200 Subject: [PATCH] Avoid duplicating keys in will/did method calls. Add accessingValueForKey() that takes a key and a block and wraps the block in willAccessValueForKey()/didAccessValueForKey(); same for will/didChangeValueForKey. This allows writing: var foo { return accessingValueForKey(Keys.Foo) { primitiveFoo } } --- SharedCode/KeyCodable.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/SharedCode/KeyCodable.swift b/SharedCode/KeyCodable.swift index 511afd8..3bbff62 100644 --- a/SharedCode/KeyCodable.swift +++ b/SharedCode/KeyCodable.swift @@ -22,6 +22,12 @@ extension KeyCodable where Self: ManagedObject, Keys.RawValue == String { didAccessValueForKey(key.rawValue) } + public func accessingValueForKey(key: Keys, @noescape block: () -> ()) { + willAccessValueForKey(key) + block() + didAccessValueForKey(key) + } + public func willChangeValueForKey(key: Keys) { (self as ManagedObject).willChangeValueForKey(key.rawValue) } @@ -42,6 +48,12 @@ extension KeyCodable where Self: ManagedObject, Keys.RawValue == String { return changedValues()[key.rawValue] } + public func changingValueForKey(key: Keys, @noescape block: () -> ()) { + willChangeValueForKey(key) + block() + didChangeValueForKey(key) + } + public func committedValueForKey(key: Keys) -> AnyObject? { return committedValuesForKeys([key.rawValue])[key.rawValue] }