Skip to content
This repository was archived by the owner on Jun 11, 2018. It is now read-only.

Repository files navigation

Build statusCoverage StatusCocoaPods CompatibleCarthage compatiblePlatform

ValueCoding

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
structFoo:ValueCoding{typealiasCoder=FooCoderletbar:String}classFooCoder:NSObject,NSCoding,CodingType{enumKeys:String{case bar ="bar"}letvalue:Foorequiredinit(_ v:Foo){
value = v
}requiredinit?(coder aDecoder:NSCoder){letbar= 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.

letdata=NSKeyedArchiver.archivedDataWithRootObject(foo.encoded)

and likewise, decoding from unarchiving can be done:

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

letfoos=Set(arrayLiteral:Foo(),Foo(),Foo())letdata=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.

letfoos=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'

About

Swift protocols for encoding/decoding value types.

Resources

Stars

60 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages