CodableValue

6.0.2

Decode UIColor and UIImage (UIKit) and NSColor and NSImage (AppKit) by utilizing Swift's Property Wrappers included in Swift 5.1
Denis5161/CodableValue

What's New

DocC support

2024-04-03T10:41:25Z

This release adds DocC support using the Swift Package Index to host the documentation.

CodableValue

Decode UIColor and UIImage (UIKit), and NSColor and NSImage (AppKit) by utilizing Swift's Property Wrappers included in Swift 5.1

Swift Package Manager compatible

GitHub last commit GitHub license

Installation

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.

Requirements

  • Swift 5.3+
  • iOS 13.0+ || macOS 11+ || watchOS 6.3+

How to Use

@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
}

Or in AppKit. For example:

@CodableValue var image: NSImage?
@CodableValue var color: NSColor

Good to Know

CodableValue conforms to Equatable, if the wrapped value also conforms to it.

CodableValue conforms to Hashable, if the wrapped value also conforms to it.

Purpose of this Package

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.

License

Provided under the MIT License. See LICENSE for more info.

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Oct 22 2024 01:29:16 GMT-0900 (Hawaii-Aleutian Daylight Time)