Skip to content

Commit

Permalink
Trying to fix issue with Codable transformation to and from base type T
Browse files Browse the repository at this point in the history
  • Loading branch information
admkopec committed Feb 13, 2024
1 parent 4d04acc commit 7b8fe9a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Sources/ManagedModels/SchemaCompatibility/CodableBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ final class CodableBox<T: Codable>: NSObject, NSCopying {

override func transformedValue(_ value: Any?) -> Any? {
// value is the box
guard let value else { return nil }
guard var value else { return nil }
if let baseTyped = value as? T {
value = CodableBox<T>(baseTyped)
}
guard let typed = value as? CodableBox<T> else {
assertionFailure("Value to be transformed is not the box? \(value)")
return nil
Expand All @@ -77,7 +80,7 @@ final class CodableBox<T: Codable>: NSObject, NSCopying {
override func reverseTransformedValue(_ value: Any?) -> Any? {
guard let value else { return nil }
guard let data = value as? Data else { return nil }
return CodableBox<T>(data: data)
return CodableBox<T>(data: data)?.value
}
}
}

0 comments on commit 7b8fe9a

Please sign in to comment.