Skip to content

Instantly share code, notes, and snippets.

@abesmon
Created May 18, 2022 17:40
Show Gist options
  • Save abesmon/87c4c2dfc6bb5f0b997210bfd9d79279 to your computer and use it in GitHub Desktop.
Save abesmon/87c4c2dfc6bb5f0b997210bfd9d79279 to your computer and use it in GitHub Desktop.
property wrapper for codable color hex string
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