sindresorhus / Defaults

💾 Swifty and modern UserDefaults
https://swiftpackageindex.com/sindresorhus/Defaults/documentation/defaults
MIT License
1.93k stars 115 forks source link

accentColor didn't work with Defaults #139

Closed owenzhao closed 1 year ago

owenzhao commented 1 year ago

I set accent color to system orange in assets folder. And in code

static let traceColor = Defaults.Key<Color>("traceColor", default: Color.orange) // works
static let traceColor = Defaults.Key<Color>("traceColor", default: Color.accentColor) // doesn't work

when reset the default key,

Defaults.reset(.traceColor)

The accentColor set by reset is the system default blue instead my own setting orange. Any idea?

Defaults 7.2.0 Xcode 14.3.1 (14E300b) iOS 16.5

sindresorhus commented 1 year ago

I don't think there's much we can do about this other than documenting the behavior. Color is not directly serializable, so the serialization works by first converting to UIColor/NSColor and then using the native color serialization.

As a workaround, you could store UIColor.tintColor and then just wrap it in Color() where you use it.

owenzhao commented 1 year ago
static let traceColor = Defaults.Key<Color>("traceColor", default: Color(uiColor: .tintColor))

Suggested worked. Thank you and please add this to the document as a workaround.