Skip to content
This repository was archived by the owner on Jun 11, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ Carthage/Build
.fastlane/report.xml
.fastlane/xcodebuild-data
*.coverage.txt
docs
17 changes: 17 additions & 0 deletions .jazzy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
author_name: Daniel Thorpe
author_url: http://danthorpe.me
module_name: ValueCoding
module_version: 1.1.0
github_url: https://github.com/danthorpe/ValueCoding
readme: README.md
podspec: ValueCoding.podspec

swift_version: 2.1
xcodebuild-arguments: -scheme,ValueCoding-iOS

custom_categories:
- name: ValueCoding
children:
- CodingType
- ValueCoding
- SequenceType
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![](header.png)
![](https://raw.githubusercontent.com/danthorpe/ValueCoding/development/header.png)

[![Build status](https://badge.buildkite.com/482fd5558d9ccf05b669c55f40450166033522f32314a1bbb2.svg)](https://buildkite.com/blindingskies/valuecoding)
[![codecov.io](http://codecov.io/github/danthorpe/ValueCoding/coverage.svg?branch=development)](http://codecov.io/github/danthorpe/ValueCoding?branch=development)
Expand Down Expand Up @@ -47,13 +47,13 @@ class FooCoder: NSObject, NSCoding, CodingType {

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

```swift
let data = NSKeyedArchiver.archivedDataWithRootObject(foo.encoded)
```

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

```swift
if let foo = Foo.decode(NSKeyedUnarchiver.unarchiveObjectWithData(data)) {
Expand All @@ -70,14 +70,19 @@ 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.
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.

```swift
let foos = Foo.decode(NSKeyedUnarchiver.unarchiveObjectWithData(data) as? [AnyObject])
```
### CoderType Examples

The [Money](https://github.com/danthorpe/Money) framework contains more examples of implementing `ValueCoding`. Including the generic type [`FXTransactionCoder`](https://github.com/danthorpe/Money/blob/development/Money/Shared/FX/FX.swift#L467).

The [YapDatabaseExtensions](https://github.com/danthorpe/YapDatabaseExtension) framework relies heavily on `ValueCoding`. For more examples of generic where constraints see its [Functional API](https://github.com/danthorpe/YapDatabaseExtensions/tree/development/YapDatabaseExtensions/Shared/Functional).

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

```ruby
pod 'ValueCoding'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "65E766141BDFA84200889368"
BuildableName = "ValueCoding-tvOS.framework"
BuildableName = "ValueCoding.framework"
BlueprintName = "ValueCoding-tvOS"
ReferencedContainer = "container:ValueCoding.xcodeproj">
</BuildableReference>
Expand Down Expand Up @@ -43,7 +43,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "65E766141BDFA84200889368"
BuildableName = "ValueCoding-tvOS.framework"
BuildableName = "ValueCoding.framework"
BlueprintName = "ValueCoding-tvOS"
ReferencedContainer = "container:ValueCoding.xcodeproj">
</BuildableReference>
Expand All @@ -65,7 +65,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "65E766141BDFA84200889368"
BuildableName = "ValueCoding-tvOS.framework"
BuildableName = "ValueCoding.framework"
BlueprintName = "ValueCoding-tvOS"
ReferencedContainer = "container:ValueCoding.xcodeproj">
</BuildableReference>
Expand All @@ -83,7 +83,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "65E766141BDFA84200889368"
BuildableName = "ValueCoding-tvOS.framework"
BuildableName = "ValueCoding.framework"
BlueprintName = "ValueCoding-tvOS"
ReferencedContainer = "container:ValueCoding.xcodeproj">
</BuildableReference>
Expand Down
26 changes: 22 additions & 4 deletions ValueCoding/ValueCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ encode/decode value types.
*/
public protocol CodingType {

/**
The type of the composed value, ValueType

Bear in mind that there are no constraints on this
type. However, in reality when working with generic
types which require coding, it will be necessary to
constrain your generic clauses like this:

```swift
func foo<T: ValueCoding where T.Coder.ValueType == T>()
```

- see: ValueCoding
*/
typealias ValueType

/// The value type which is being encoded/decoded
Expand All @@ -32,6 +46,12 @@ A generic protocol for value types which require
coding.
*/
public protocol ValueCoding {

/**
The Coder which implements CodingType

- see: CodingType
*/
typealias Coder: CodingType
}

Expand All @@ -48,8 +68,7 @@ extension CodingType where ValueType: ValueCoding, ValueType.Coder == Self {
}
}

extension SequenceType
where
extension SequenceType where
Generator.Element: CodingType {

/// Access the values from a sequence of coders.
Expand Down Expand Up @@ -106,8 +125,7 @@ extension ValueCoding where Coder: NSCoding, Coder.ValueType == Self {
}
}

extension SequenceType
where
extension SequenceType where
Generator.Element: ValueCoding,
Generator.Element.Coder: NSCoding,
Generator.Element.Coder.ValueType == Generator.Element {
Expand Down