Git Product home page Git Product logo

valuecoding's Introduction

Build status codecov.io Cocoapods Compatible Carthage compatible Platform

ValueCoding

ValueCoding is a simple pair of protocols to support the coding of Swift value types.

It works by allowing a value type, which must conform to ValueCoding to define via a typealias its coder. The coder is another type which implements the CoderType protocol. This type will typically be an NSObject which implements NSCoding and is an adaptor which is responsible for encoding and decoding the properties of the value.

A minimal example for a simple struct is shown below:

import ValueCoding

struct Foo: ValueCoding {
    typealias Coder = FooCoder
    let bar: String
}

class FooCoder: NSObject, NSCoding, CodingType {

    enum Keys: String {
        case Bar = "bar"
    }

    let value: Foo

    required init(_ v: Foo) {
        value = v
    }

    required init?(coder aDecoder: NSCoder) {
        let bar = aDecoder.decodeObjectForKey(Keys.Bar.rawValue) as? String
        value = Foo(bar: bar!)
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(value.bar, forKey: Keys.Bar.rawValue)
    }
}

If you are converting existing models from classes to values types, the NSCoding methods should look familiar, and hopefully you are able to reuse your existing code.

The framework provides static methods and properties for types which conform to ValueCoding with correct archivers. Therefore, given a value of Foo, you can encode it ready for serialization using NSKeyedArchiver.

let data = NSKeyedArchiver.archivedDataWithRootObject(foo.encoded)

and likewise, unarchiving (and decoding) can be done:

if let foo = Foo.decode(NSKeyedUnarchiver.unarchiveObjectWithData(data)) {
    // etc, unarchive returns optionals when working with a single item.
}

These methods can also be used if composing value types inside other types which require encoding.

When working with sequences of values, use the encoded property on the sequence.

let foos = Set(arrayLiteral: Foo(), Foo(), Foo())
let data = NSKeyedArchiver.archivedDataWithRootObject(foos.encoded)

When decoding an NSArray, perform a conditional cast to [AnyObject] before passing it to decode. The result will be an Array<Foo> which will be empty if the object was not cast successfully. In addition, any members of [AnyObject] which did not decode will filtered from the result. This means that the length of the result will be less than the original encoded array if there was an issue decoding.

let foos = Foo.decode(NSKeyedUnarchiver.unarchiveObjectWithData(data) as? [AnyObject])

Installation

ValueCoding builds as a cross platform (iOS, OS X, watchOS) extension compatible framework. It is also available via CocoaPods

pod ‘ValueCoding’

valuecoding's People

Contributors

danthorpe 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.