Giter Site home page Giter Site logo

codablevalue's Introduction

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.

codablevalue's People

Contributors

denis5161 avatar

Stargazers

 avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.