/SwiftCodableExtensions

CodableExtension contains helper methods for Decodable objects. The Codable documention from Apple is already very good and the Codable library from the Standard Swift Library in my opinion is exceptional. Everything can always be made easier, therefore I have made these extensions which allow for me to write even less code than what we do already.

Primary LanguageSwiftMIT LicenseMIT

CodableExtensions

CodableExtension contains helper methods for Decodable objects.

The Codable documention from Apple is already very good and the Codable library from the Standard Swift Library in my opinion is exceptional.

Everything can always be made easier, therefore I have made these extensions which allow for me to write even less code than what we do already.

Contents

Introduction

The examples below are based upon a Person object.

The Person object has one member property and this is labelled name with a type of String?.

Very simple huh!

public class Person: Codable {

    public var name: String?

    public init()  {}

    ... 

    /// decode and encode methods

    ...

}

As you can see my Person object implements the swift 4 Codable protocol.

Aim: to reduce the boiler plate code written

From:

do {

    let data = try JSONSerialization.data(withJSONObject: withDictionary)
    return try JSONDecoder().decode(self, from: data)

} catch {
    return nil
}

To

guard let person = Person.load(withDictionary: dictionary ) else {
    return nil
}

Requirements

  • Xcode 9.2
  • Minimum iOS Deploment Target 9.0
  • Swift 4.0

Installation

CodableExtensions doesn't contain any external dependencies.

These are currently the supported options:

Not yet supported but coming very soon

Not yet supported but coming very soon

Tested with swift build --version: Swift 4.0.0-dev (swiftpm-13126)

Create a Package.swift file.

// swift-tools-version:4.0

import PackageDescription

let package = Package(
    name: "PACKAGE_NAME",
    dependencies: [
        .package(url: "https://github.com/davidthorn/SwiftCodableExtensions.git", from: "0.1.0")
    ],
    targets: [
        .target(name: "PACKAGE_NAME", dependencies: [
        "CodableExtensions"
        ])
    ]
    )
$ swift build

Usage

To me the most basic way for someone to want to do this is with taking a simple JSON String, NSDictionary, HashableDictionary or Data object and saying hey give me a decoded object from that in one line of code.

Well with a few methods it is possible.

Decode using Foundation Data object

let jsonData: Data =  .... // Load the json Data from a server/api etc

guard let person: Person = Person.decode(withJSONData: jsonData) else {
    return nil
}

Decode using a Foundation NSDictionary

let dictionary: NSDictionary = [
    "name"  : "david"
]

guard let person: Person = Person.decode(withDictionary: dictionary ) else {
    return nil
}

Decode using a [AnyHashable:Any] Dictionary

let dictionary: [AnyHashable:Any] = [
    "name"  : "david"
]

guard let person: Person = Person.decode(withHashableDictionary: dictionary ) else {
    return nil
}

Decode using a JSON String

let jsonString = """
{
    "name" : "david"
}
        """

guard let person = Person.decode(withJsonString: jsonString) else {
    return nil
}