Implementation note to save Color in UserDefaults when using Swift UI
Save as Color → cgColor → RGB value
let userDefaults = UserDefaults.standard
let color = Color.orange
if let components = color.cgColor?.components {
userDefaults.setValue(components[0], forKey: "RColor")
userDefaults.setValue(components[1], forKey: "GColor")
userDefaults.setValue(components[2], forKey: "BColor")
}
When using, extract the value as Double type from UserDefaults and Just generate Color from RGB as below
let color = Color(.sRGB, red: RColor, green: GColor, blue: BColor, opacity: 1.0)
The app has been released! Please install it if you like. Toranpo
I started Twitter! Please follow me if you like. @yajima_tohshu
Recommended Posts