From 768502fea62b14b1afadf32ed65b9c2ba60062bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20He=C3=9F?= Date: Sat, 7 Oct 2023 15:17:37 +0200 Subject: [PATCH] Fix issue #22, optional handling Finally able to understand the issue. The typed optional .none is properly bridged to NSNull, but the coercion isn't even necessary. --- .../PersistentModel/PersistentModel+KVC.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift b/Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift index 4bb2bed..c83eb0d 100644 --- a/Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift +++ b/Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift @@ -31,7 +31,15 @@ public extension PersistentModel { where T: Codable & CoreDataPrimitiveValue & AnyOptional { willChangeValue(forKey: key); defer { didChangeValue(forKey: key) } - setPrimitiveValue(value, forKey: key) + + // While `nil` is properly bridged to `NSNull`, this is still necessary + // because `T` is the Optional structure, NOT the value type. I think :-) + if value.isSome { + setPrimitiveValue(value.value, forKey: key) + } + else { + setPrimitiveValue(nil, forKey: key) + } } @inlinable func getValue(forKey key: String) -> T