Supports optional @Managed properties

This commit is contained in:
Riley Testut
2022-11-15 16:52:17 -06:00
parent b181b2e7a4
commit ac8fb8851f

View File

@@ -50,4 +50,23 @@ struct Managed<ManagedObject>
return result
}
// Optionals
subscript<Wrapped, T>(dynamicMember keyPath: KeyPath<Wrapped, T>) -> T? where ManagedObject == Optional<Wrapped>
{
var result: T?
if let context = self.managedObjectContext
{
context.performAndWait {
result = self.wrappedValue?[keyPath: keyPath] as? T
}
}
else
{
result = self.wrappedValue?[keyPath: keyPath] as? T
}
return result
}
}