Awesome
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
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'