SwiftData iCloud AttributedString Platform Color compatibility

Hi

Given a simple multiplatform app about Mushrooms, stored in SwiftData, hosted in iCloud using a TextEditor

@Model
final class Champignon: Codable {
    var nom: String = ""
../..
    @Attribute(.externalStorage)
    var attributedStringData: Data = Data()
    var attributedString: AttributedString {
        get {
            do {
                return try JSONDecoder().decode(AttributedString.self,
                                                from: attributedStringData)
            } catch {
                return AttributedString("Failed to decode AttributedString: \(error)")
            }
        }
        set {
            do {
                self.attributedStringData = try JSONEncoder().encode(newValue)
            } catch {
                print("Failed to encode AttributedString: \(error)")
            }
        }
    }
../..

Computed attributedString is used in a TextEditor

private var textEditorView: some View {
        Section {
            TextEditor(text: $model.attributedString)
        } header: {
            HStack {
                Text("TextEditor".localizedUppercase)
                    .foregroundStyle(.secondary)
                Spacer()
            }
        }
    }

Plain Text encode, decode and sync like a charm through iOS and macOS

Use of "FontAttributes" (Bold, Italic, …) works the same

But use of "ForegroundColorAttributes" trigger an error :

Failed to decode AttributedString: dataCorrupted(Swift.DecodingError.Context(codingPath: [_CodingKey(stringValue: "Index 3", intValue: 3), AttributeKey(stringValue: "SwiftUI.ForegroundColor", intValue: nil), CodableBoxCodingKeys(stringValue: "value", intValue: 1)], debugDescription: "Platform color is not available on this platform", underlyingError: nil))

Is there a way to encode/decode attributedString data platform conditionally ? Or another approach ?

Thanks for advices

SwiftData iCloud AttributedString Platform Color compatibility
 
 
Q