Why is my code erroring when I try to store a color to UserDefaults and then save it back to background color?

26 views Asked by At
func setColor(col: UIColor) {
    let colDict: Dictionary<Int, UIColor> = [1: col]
    defaults.set(colDict, forKey: "color")
    if let dict = defaults.dictionary(forKey: "color") as? NSDictionary {
        let color = dict[1]
        view.backgroundColor = color as? UIColor
    }
}

this is my error: Thread 1: "Attempt to insert non-property list object {\n 1 = "UIExtendedSRGBColorSpace 1 0 0 1";\n} for key color"

I have tried using JSONEncoders and Arrays but it gets the same error

0

There are 0 answers