Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- extension UIColor {
- func components() -> [CGFloat] {
- var r = CGFloat(0)
- var g = CGFloat(0)
- var b = CGFloat(0)
- var a = CGFloat(0)
- getRed(&r, green: &g, blue: &b, alpha: &a)
- return [r,g,b,a]
- }
- convenience init?(components: [CGFloat]) {
- guard components.count == 4 else {
- return nil
- }
- self.init(red: components[0],
- green: components[1],
- blue: components[2],
- alpha: components[3])
- }
- }
- let colors: [UIColor] = [.cyan, .magenta, .yellow, .black]
- let serialized = colors.map { $0.components() }
- UserDefaults.standard.set(serialized, forKey: "SerializedColors")
- let restored = UserDefaults.standard.value(forKey: "SerializedColors") as? [[CGFloat]]
- for components in restored ?? [] {
- print(UIColor(components: components) ?? "not a color")
- }
Advertisement
Add Comment
Please, Sign In to add comment