Error booting app with Transformer in SwiftData Class

Hi folks, I'm trying to use a Transformer in a SwiftData class, but everytime i boot the app in the visionOS simulator it shows an error in main.

Error: Thread 1: Fatal error: Application must register a ValueTransformer for NSAttributedStringTransformer

Here's my main file:

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup(id: "main") {
            InfoWindow()
            .modelContainer(for: Document.self)
        }
        .windowResizability(.contentSize)
    }
}

Here's the class file:

import SwiftData

@Model
final class Document {
    @Attribute(.unique) var id: String
    @Attribute(.transformable(by: NSAttributedStringTransformer.self)) var content: NSAttributedString
    var name: String
    var history: [String]
    
    init(content: NSAttributedString, name: String, history: [String]) {
        self.id = UUID().uuidString
        self.content = content
        self.name = name
        self.history = history
    }
}

extension Document: Identifiable { }

}

And the Transformer:


extension NSValueTransformerName {
    static let nsAttributedStringTransformer = NSValueTransformerName(rawValue: "NSAttributedStringTransformer")
}

@objc(NSAttributedStringTransformer)
class NSAttributedStringTransformer: NSSecureUnarchiveFromDataTransformer {
    override class func allowsReverseTransformation() -> Bool {
        return true
    }

    override class var allowedTopLevelClasses: [AnyClass] {
        return [NSAttributedString.self]
    }

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

    override func reverseTransformedValue(_ value: Any?) -> Any? {
        guard let attributedString = value as? NSAttributedString else {
            return nil
        }
        return attributedString.toNSData()
    }

    override func transformedValue(_ value: Any?) -> Any? {
        guard let data = value as? NSData else {
            return nil
        }
        return data.toAttributedString()
    }
}

private extension NSData {
    func toAttributedString() -> NSAttributedString? {
        let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
            .documentType: NSAttributedString.DocumentType.rtf,
            .characterEncoding: String.Encoding.utf8
        ]
        return try? NSAttributedString(data: Data(referencing: self),
                                       options: options,
                                       documentAttributes: nil)
    }
}

private extension NSAttributedString {
    func toNSData() -> NSData? {
        let options: [NSAttributedString.DocumentAttributeKey: Any] = [
            .documentType: NSAttributedString.DocumentType.rtf,
            .characterEncoding: String.Encoding.utf8
        ]
        let range = NSRange(location: 0, length: length)
        guard let data = try? data(from: range, documentAttributes: options) else {
            return nil
        }
        return NSData(data: data)
    }
}

Been stuck in this issue for a while now and would appreciate if anyone with more experience could help!

Error booting app with Transformer in SwiftData Class
 
 
Q