Skip to content

Commit 768502f

Browse files
committed
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.
1 parent 86720f5 commit 768502f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ public extension PersistentModel {
3131
where T: Codable & CoreDataPrimitiveValue & AnyOptional
3232
{
3333
willChangeValue(forKey: key); defer { didChangeValue(forKey: key) }
34-
setPrimitiveValue(value, forKey: key)
34+
35+
// While `nil` is properly bridged to `NSNull`, this is still necessary
36+
// because `T` is the Optional structure, NOT the value type. I think :-)
37+
if value.isSome {
38+
setPrimitiveValue(value.value, forKey: key)
39+
}
40+
else {
41+
setPrimitiveValue(nil, forKey: key)
42+
}
3543
}
3644
@inlinable
3745
func getValue<T>(forKey key: String) -> T

0 commit comments

Comments
 (0)