1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Swift】SwiftUIでColorをUserDefaultsに保存(メモ)

Last updated at Posted at 2020-10-07

SwiftUI使用時ColorをUserDefaultsに保存する実装メモ

#環境

  • Xcode: 12.0
  • Swift5

#実装例
Color→cgColor→RGB値にして保存

        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")
        }

使用時はUserDefaultsからDouble型で値を取り出し、
以下のようにRGBからColorを生成するだけ

let color = Color(.sRGB, red: RColor, green: GColor, blue: BColor, opacity: 1.0)
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?