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!
Post
Replies
Boosts
Views
Activity
Hi folks, i've been getting a weird not helpful error when trying to use SwiftData and WindowGroup, here's the error:
Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
And here's my app file:
struct DocumentApp: App {
private var container = try! ModelContainer(for: Document.self)
var body: some Scene {
WindowGroup(id: "main") {
InfoWindow()
}
.modelContainer(container)
.windowResizability(.contentSize)
WindowGroup(id: "note", for: Document.Note.ID.self) { $detailID in
@Query var documents: [Document]
let openDoc = documents.first { $0.id == "\($detailID)" }
NoteView(note: openDoc?.notes)
}
.defaultSize(CGSize(width: 550, height: 550))
.windowResizability(.contentSize)
ImmersiveSpace(id: "board") {
ProjectBoard()
.modelContainer(container)
}
.immersionStyle(selection: .constant(.mixed), in: .mixed)
}
}
Thanks to anyone who took the time to read this post!