Decode UIColor and UIImage (UIKit), and NSColor and NSImage (AppKit) by utilizing Swift's Property Wrappers included in Swift 5.1
Only Swift Package Manager is supported as of this release. I have no plans to support other package managers, like CocoaPods or Carthage.
Add these lines inside an existing Packages.swift
file:
dependencies: [
.package(url: "https://github.com/Denis5161/CodableValue.git", from: "6.0.1")
]
Or use Xcode to add a package. See Swift Package Documentation for more info.
- Swift 5.3+
- iOS 13.0+ || macOS 11+ || watchOS 6.3+
@CodableValue
can encode/ decode the following types:
- images (UIImage/NSImage)
- colors (UIColor/NSColor)
The property wrapper is designed to be very transparent to the API user. Optionals work with the same property wrapper.
Declare a property wrapper on a property that you wish to use. For example:
@CodableValue var image: UIImage?
@CodableValue var swiftImage = UIImage(systemName: "swift")!
@CodableValue var color: UIColor = .systemBlue
When initializing the values, for example in an init method of a custom type:
init(image: UIImage?, color: UIColor) {
self.image = image
self.color = color
}
@CodableValue var image: NSImage?
@CodableValue var color: NSColor
CodableValue
conforms to Equatable
, if the wrapped value also conforms to it.
CodableValue
conforms to Hashable
, if the wrapped value also conforms to it.
The purpose of this package was for me to learn about Property Wrappers. Some types don't conform to Codable and can't synthesize the needed methods automatically. When a data model only has a few of these properties - while the rest support Codable - then it's better to have the compiler synthesize Codable conformance automatically instead of writing a lot of boilerplate code for properties that already support it.
Adding Encodable conformance to a UIKit type works easily inside of an extension on that type. But Decodable conformance does not work, because it is a required initializer and I'm guessing the implementation details of UIKit classes stop one from creating the init(from decoder: Decoder)
method inside an extension.
I have spent a considerable amount of time fiddling with this, and this solution seems to be the most stable and easiest to understand from an API perspective.
Provided under the MIT License. See LICENSE for more info.