Created
May 18, 2022 17:40
-
-
Save abesmon/87c4c2dfc6bb5f0b997210bfd9d79279 to your computer and use it in GitHub Desktop.
property wrapper for codable color hex string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@propertyWrapper | |
struct UIColorHexString: Codable { | |
private var stringValue: String | |
var wrappedValue: UIColor { | |
get { UIColor(hexString: stringValue) } | |
set { stringValue = newValue.hexString } | |
} | |
init(_ color: UIColor) { | |
self.stringValue = color.hexString | |
} | |
init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
stringValue = try container.decode(String.self) | |
} | |
func encode(to encoder: Encoder) throws { | |
var container = encoder.singleValueContainer() | |
try container.encode(stringValue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment