Pack 1.0.0

Pack 1.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Dec 2017
SwiftSwift Version 4.0
SPMSupports SPM

Maintained by Anton Romanov.



Pack 1.0.0

  • By
  • Istered

Pack

Build Status
Version
License
Platform
Carthage compatible

Introduction

Pack is a tiny wrapper around Encodable/Decodable protocols that let you write code in the same fashion (well, almost) as you would write it using ObjectMapper. ObjectMapper is the main source of inspiration for this library, so you'll find that some protocols and objects have the same names and structures. It is made so you can easily migrate your existing ObjectMapper code to Pack.

Motivation

Why would you use this library? In some cases default implementation / code generation of Encodable/Decodable gives you great results and keeps your code as short as possible. However, if you need to slightly customize the process of mapping you'll write a lot of repetative code like this:

class Item: Codable {
    let userId: String
    let position: Int
    let score: Int

    private enum CodingKeys: String, CodingKey {
        case userId = "id"
        case position
        case score
    }
    
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        
        userId = try container.decodeIfPresent(String.self, forKey: .userId) ?? ""
        position = try container.decodeIfPresent(Int.self, forKey: .position) ?? 0
        score = try container.decodeIfPresent(Int.self, forKey: .score) ?? 0
    }

    func encode(to encoder: Encoder) throws {
        let container = try encoder.container(keyedBy: CodingKeys.self)

        try encode(String.self, forKey: .userId)
        try encode(Int.self, forKey: .position)
        try encode(Int.self, forKey: .score)
    }
}

So istead of that Pack lets you write:

class Item: Mappable {
    var userId = ""
    var position = 0
    var score = 0
    
    required init() {
    }

    func mapping(map: Map) {
        userId <- map["id"]
        position <- map["position"]
        score <- map["score"]
    }
}

Since Pack is just a wrapper any class that has implemented Mappable protocol also has compability with code that is using Encodable/Decodable already.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate Pack into your Xcode project using CocoaPods, specify it in your Podfile:

use_frameworks!

pod 'Pack'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Pack into your Xcode project using Carthage, specify it in your Cartfile:

github "istered/Pack"

Run carthage update to build the framework and drag the built Pack.framework into your Xcode project.

Author

Anton Romanov

License

Pack is available under the MIT license. See the LICENSE file for more info.