3

我有一组颜色,我想保存到 UserDefaults。我有灰色、绿色、橙色和红色的默认值,但我想允许颜色选择器更改绿色橙色和红色。默认值在我的模拟器中有效并显示,但是当我尝试更改数组中的颜色时,我收到错误“尝试插入非属性列表对象(\n“50% 灰色”,\n 绿色,\n“ kCGColorSpaceModelRGB 0.647194 0.881984 0.980039 1 ",\n red\n) 用于键 SavedColors。我相信这是因为颜色选择器试图插入不同类型的颜色?看起来它可能正在尝试插入 CGColor 或 CGColorSpace?

这是我的项目代码:

import SwiftUI
import Foundation
import Combine

class UserSettings: ObservableObject {
    @Published var colors: [Color] {
        didSet {
            UserDefaults.standard.set(colors, forKey: "SavedColors")
        }
    }
    init() {
        self.colors = UserDefaults.standard.object(forKey: "SavedColors") as? [Color] ?? [Color.gray.opacity(0.5), Color.green, Color.orange, Color.red]
    }
}

struct CustomizeView: View {
    @ObservedObject var savedColors = UserSettings()
    var body: some View {
        NavigationView {
            Form {
                if #available(iOS 14.0, *) {
                    ColorPicker("Select low priority color", selection: $savedColors.colors[1])
                } else {
                    Text("Select low priority color")
                }
                if #available(iOS 14.0, *) {
                    ColorPicker("Select normal priority color", selection: $savedColors.colors[2])
                } else {
                    Text("Select normal priority color")
                }
                if #available(iOS 14.0, *) {
                    ColorPicker("Select high priority color", selection: $savedColors.colors[3])
                } else {
                    Text("Select high priority color")
                }
            }.navigationBarTitle("Customize", displayMode: .inline)
      }
}
4

1 回答 1

1

你好!

我可以在 MacOS 上通过这种转换来保存 SwiftUI 颜色:

extension Color {

    /// Explicitly extracted Core Graphics color
    /// for the purpose of reconstruction and persistance.
    var cgColor_: CGColor {
        NSColor(self).cgColor
    }
}

extension UserDefaults {
    func setColor(_ color: Color, forKey key: String) {
        let cgColor = color.cgColor_
        let array = cgColor.components ?? []
        set(array, forKey: key)
    }

    func color(forKey key: String) -> Color {
        guard let array = object(forKey: key) as? [CGFloat] else { return .accentColor }
        let color = CGColor(colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!, components: array)!
        return Color(color)
    }
}
于 2022-01-25T15:40:06.063 回答