Latest 3.0.0
Homepage https://github.com/danthorpe/ValueCoding
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, watchos 2.0, requires ARC
Authors

ValueCoding

Build status
Coverage Status
CocoaPods Compatible
Carthage compatible
Platform

Please note that ValueCoding is marked as deprecated, as it has been rendered obsolete by Apple introducing the Codable protocol with Swift 4.0. The project has been mothballed, and will no longer be maintained.

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 valid coders. Therefore, given a value of Foo, you can encode it ready for archiving using NSKeyedArchiver.

let data = NSKeyedArchiver.archivedDataWithRootObject(foo.encoded)

and likewise, decoding from unarchiving can be done:

if let foo = Foo.decode(NSKeyedUnarchiver.unarchiveObjectWithData(data)) {
    // etc, decode 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 be 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])

CoderType Examples

The Money framework contains more examples of implementing ValueCoding. Including the generic type FXTransactionCoder.

The YapDatabaseExtensions framework relies heavily on ValueCoding. For more examples of generic where constraints see its Functional API.

Installation

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

pod 'ValueCoding'

Latest podspec

{
    "name": "ValueCoding",
    "version": "3.0.0",
    "summary": "Swift protocols for encoding/decoding value types.",
    "description": "ValueCoding is a simple pair of protocols to support the archivingnand unarchiving of Swift value types.nnIt works by allowing a value type, which must conform to ValueCodingnto define via a typealias its archiver. The archiver is another typenwhich implements the ArchiverType protocol. This type will typicallynbe an NSObject which implements NSCoding and is an adaptor which isnresponsible for encoding and decoding the properties of the value.",
    "homepage": "https://github.com/danthorpe/ValueCoding",
    "license": "MIT",
    "authors": {
        "Daniel Thorpe": "@danthorpe"
    },
    "source": {
        "git": "https://github.com/danthorpe/ValueCoding.git",
        "tag": "3.0.0"
    },
    "module_name": "ValueCoding",
    "social_media_url": "https://twitter.com/danthorpe",
    "requires_arc": true,
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "source_files": "Sources/*.swift",
    "pushed_with_swift_version": "3.2"
}

Pin It on Pinterest

Share This