diff --git a/Sources/ObjectPublisher.swift b/Sources/ObjectPublisher.swift index ce2cfde..7412fc1 100644 --- a/Sources/ObjectPublisher.swift +++ b/Sources/ObjectPublisher.swift @@ -356,7 +356,7 @@ extension ObjectPublisher where O: NSManagedObject { public func value(forKeyPath keyPath: KeyPath) -> V! { let key = String(keyPath: keyPath) - return self.snapshot?.dictionaryForValues()[key] as! V? + return self.snapshot?.dictionaryForValues()[key] as? V } } diff --git a/Sources/ObjectSnapshot.swift b/Sources/ObjectSnapshot.swift index 34f7255..259c536 100644 --- a/Sources/ObjectSnapshot.swift +++ b/Sources/ObjectSnapshot.swift @@ -120,11 +120,15 @@ public struct ObjectSnapshot: ObjectRepresentation, Hashable { } + // MARK: FilePrivate + + fileprivate var values: [String: Any] + + // MARK: Private private let id: O.ObjectID private let context: NSManagedObjectContext - private var values: [String: Any] private var valuesRef: NSDictionary { @@ -153,7 +157,16 @@ extension ObjectSnapshot where O: NSManagedObject { public func value(forKeyPath keyPath: KeyPath) -> V! { let key = String(keyPath: keyPath) - return self.values[key] as! V? + return self.values[key] as? V + } + + /** + Mutates the value for the property identified by a given key. + */ + public mutating func setValue(_ value: V!, forKeyPath keyPath: KeyPath) { + + let key = String(keyPath: keyPath) + self.values[key] = value } } @@ -187,7 +200,7 @@ extension ObjectSnapshot where O: CoreStoreObject { get { let key = String(keyPath: member) - return self.values[key] as! V? + return self.values[key] as? V } set { @@ -221,7 +234,7 @@ extension ObjectSnapshot where O: CoreStoreObject { get { let key = String(keyPath: member) - return self.values[key] as! V? + return self.values[key] as? V } set { @@ -238,7 +251,7 @@ extension ObjectSnapshot where O: CoreStoreObject { get { let key = String(keyPath: member) - guard let id = self.values[key] as! D.ObjectID? else { + guard let id = self.values[key] as? D.ObjectID else { return nil } diff --git a/Sources/PartialObject.swift b/Sources/PartialObject.swift index 9d42664..82f8731 100644 --- a/Sources/PartialObject.swift +++ b/Sources/PartialObject.swift @@ -191,7 +191,7 @@ public struct PartialObject { */ public func value(for property: (O) -> TransformableContainer.Optional) -> V? { - return self.rawObject.value(forKey: property(O.meta).keyPath) as! V? + return self.rawObject.value(forKey: property(O.meta).keyPath) as? V } /** @@ -212,7 +212,7 @@ public struct PartialObject { */ public func primitiveValue(for property: (O) -> TransformableContainer.Optional) -> V? { - return self.rawObject.primitiveValue(forKey: property(O.meta).keyPath) as! V? + return self.rawObject.primitiveValue(forKey: property(O.meta).keyPath) as? V } /** diff --git a/Sources/Transformable.swift b/Sources/Transformable.swift index f26ed6c..182f0bc 100644 --- a/Sources/Transformable.swift +++ b/Sources/Transformable.swift @@ -380,7 +380,7 @@ public enum TransformableContainer { return customGetter(PartialObject(object)) } - return object.value(forKey: self.keyPath) as! V? + return object.value(forKey: self.keyPath) as? V } } set { @@ -489,7 +489,7 @@ public enum TransformableContainer { } customSetter( PartialObject(rawObject), - newValue as! V? + newValue as? V ) } }