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 diff --git a/Tests/ManagedModelTests/CoreDataAssumptionsTests.swift b/Tests/ManagedModelTests/CoreDataAssumptionsTests.swift index 3da87a0..204036d 100644 --- a/Tests/ManagedModelTests/CoreDataAssumptionsTests.swift +++ b/Tests/ManagedModelTests/CoreDataAssumptionsTests.swift @@ -67,4 +67,20 @@ final class CoreDataAssumptionsTests: XCTestCase { XCTAssertEqual(relationship.isOrdered, copiedRelationship.isOrdered) XCTAssertEqual(relationship.maxCount, copiedRelationship.maxCount) } + + + func testAttributeValueClassIsNotEmpty() throws { + do { + let attribute = NSAttributeDescription() + attribute.name = "Hello" + attribute.attributeType = .stringAttributeType + XCTAssertEqual(attribute.attributeValueClassName, "NSString") + } + do { + let attribute = NSAttributeDescription() + attribute.name = "Hello" + attribute.attributeType = .integer16AttributeType + XCTAssertEqual(attribute.attributeValueClassName, "NSNumber") + } + } }