ValueTransformer: Unable to determine the primitive for Attribute Error

I've been struggling to get a ValueTransformer to work while developing in Xcode 16 for iOS 18. Despite thinking I had everything set up correctly, I keep encountering the following error whenever I create a tag:

let tag = Tag(name: name, color: color.toPlatformColor()) // Converts it to NSColor or UIColor
modelContext.insert(tag)
SwiftData/DataUtilities.swift:184: Fatal error: Unable to determine the primitive for Attribute - name: color, options: [transformable with Optional("ColorTransformer")], valueType: UIColor, defaultValue: UIExtendedSRGBColorSpace 0 0 1 1, hashModifier: nil

Here’s what I’m dealing with:

Tag Model:

@Model
public final class Tag: Identifiable {
  var name: String = ""
  
  @Attribute(.transformable(by: ColorTransformer.self))
  var color: PlatformColor = PlatformColor.blue

  init(name: String, color: PlatformColor) {
    self.name = name
    self.color = color
  }
}

ColorTransformer:

final class ColorTransformer: ValueTransformer {
  override func transformedValue(_ value: Any?) -> Any? {
    guard let color = value as? PlatformColor else { return nil }
    do {
      let data = try NSKeyedArchiver.archivedData(
        withRootObject: color, requiringSecureCoding: true)
      return data
    } catch {
      assertionFailure("Failed to transform `PlatformColor` to `Data`")
      return nil
    }
  }

  override func reverseTransformedValue(_ value: Any?) -> Any? {
    guard let data = value as? NSData else { return PlatformColor.black }
    do {
      let color = try NSKeyedUnarchiver.unarchivedObject(
        ofClass: PlatformColor.self, from: data as Data)
      return color
    } catch {
      assertionFailure("Failed to transform `Data` to `PlatformColor`")
      return nil
    }
  }

  override class func transformedValueClass() -> AnyClass {
    return PlatformColor.self
  }

  override class func allowsReverseTransformation() -> Bool {
    return true
  }

  public static func register() {
    ValueTransformer.setValueTransformer(ColorTransformer(), forName: .colorTransformer)
  }
}

extension NSValueTransformerName {
  static let colorTransformer = NSValueTransformerName(rawValue: "ColorTransformer")
}

Platform Alias:

#if os(macOS)
  typealias PlatformColor = NSColor
#else
  typealias PlatformColor = UIColor
#endif

The ValueTransformer is registered when the ModelContainer is created at app startup:

var sharedModelContainer: ModelContainer = {
  ColorTransformer.register()
  // Other configurations...
}()

I've also tried not aliasing the colors to see if that changes anything, but I still encounter the same issue. Any guidance or suggestions would be greatly appreciated!

ValueTransformer: Unable to determine the primitive for Attribute Error
 
 
Q