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

Commit

Permalink
2.0.0 (#24)
Browse files Browse the repository at this point in the history
* [swift/3.0]: Changes for Swift 3.0

* [2.0.0]: Sets the version

* [2.0.0]: Updates CHANGELOG
  • Loading branch information
danthorpe authored Sep 28, 2016
1 parent 1a74093 commit b8e67db
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .jazzy.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author_url": "http://danthorpe.me",
"github_url": "https://github.com/danthorpe/ValueCoding",
"module": "ValueCoding",
"module_version": "1.5.0",
"module_version": "2.0.0",
"readme": "README.md",
"swift_version": "2.3",
"xcodebuild_arguments": [
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 2.0.0
This is the Swift 3.0 compatible version.

The key change here is that `CodingType` has been renamed to `CodingProtocol` in accordance with Swift 3.0 naming style.

# 1.5.0
This is a Swift 2.3 compatible version

# 1.4.0
1. [[VCD-19](https://github.com/danthorpe/ValueCoding/pull/19)]: Recreates the project to use a single multi-platform framework target.

Expand Down
99 changes: 39 additions & 60 deletions Sources/ValueCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,30 @@

import Foundation

// MARK: - CodingType
// MARK: - CodingProtocol

/**
A generic protocol for classes which can
encode/decode value types.
*/
public protocol CodingType {
public protocol CodedValue {

/**
The type of the composed value, ValueType
The type of the composed value, Value

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:
- see: ValueCoding
*/
associatedtype Value

```swift
func foo<T: ValueCoding where T.Coder.ValueType == T>()
```
/// The value type which was coded
var value: Value { get }
}

- see: ValueCoding
*/
associatedtype ValueType

/// The value type which is being encoded/decoded
var value: ValueType { get }
/**
A generic protocol for classes which can
encode/decode value types.
*/
public protocol CodingProtocol: CodedValue {

/// Required initializer receiving the wrapped value type.
init(_: ValueType)
init(_: Value)
}

// MARK: - ValueCoding
Expand All @@ -48,35 +43,19 @@ coding.
public protocol ValueCoding {

/**
The Coder which implements CodingType
The Coder which implements CodingProtocol

- see: CodingType
- see: CodingProtocol
*/
associatedtype Coder: CodingType
associatedtype Coder: CodingProtocol
}

// MARK: - Protocol Extensions

extension CodingType where ValueType: ValueCoding, ValueType.Coder == Self {

internal static func decode(object: AnyObject?) -> ValueType? {
return (object as? Self)?.value
}

internal static func decode<S: SequenceType where S.Generator.Element: AnyObject>(objects: S?) -> [ValueType] {
return objects?.flatMap(decode) ?? []
}

internal static func decode<S: SequenceType where S.Generator.Element: SequenceType, S.Generator.Element.Generator.Element: AnyObject>(objects: S?) -> [[ValueType]] {
return objects?.flatMap(decode) ?? []
}
}

extension SequenceType where
Generator.Element: CodingType {
public extension Sequence where Iterator.Element: CodingProtocol {

/// Access the values from a sequence of coders.
public var values: [Generator.Element.ValueType] {
var values: [Iterator.Element.Value] {
return map { $0.value }
}
}
Expand All @@ -85,7 +64,7 @@ extension SequenceType where
Static methods for decoding `AnyObject` to Self, and returning encoded object
of Self.
*/
extension ValueCoding where Coder: NSCoding, Coder.ValueType == Self {
public extension ValueCoding where Coder: NSCoding, Coder.Value == Self {

/**
Decodes the value from a single decoder, if possible.
Expand All @@ -97,8 +76,8 @@ extension ValueCoding where Coder: NSCoding, Coder.ValueType == Self {
be of `Coder` type.
- returns: an optional `Self`
*/
public static func decode(object: AnyObject?) -> Self? {
return Coder.decode(object)
static func decode(_ object: AnyObject?) -> Self? {
return (object as? Coder)?.value
}

/**
Expand All @@ -110,8 +89,8 @@ extension ValueCoding where Coder: NSCoding, Coder.ValueType == Self {
- parameter objects: a `SequenceType` of `AnyObject`.
- returns: the array of values which were able to be unarchived.
*/
public static func decode<S: SequenceType where S.Generator.Element: AnyObject>(objects: S?) -> [Self] {
return Coder.decode(objects)
static func decode<S: Sequence>(_ objects: S?) -> [Self] where S.Iterator.Element: AnyObject {
return objects?.flatMap(Self.decode) ?? []
}

/**
Expand All @@ -120,8 +99,8 @@ extension ValueCoding where Coder: NSCoding, Coder.ValueType == Self {
- parameter objects: a `SequenceType` of `SequenceType` of `AnyObject`.
- returns: the array of arrays of values which were able to be unarchived.
*/
public static func decode<S: SequenceType where S.Generator.Element: SequenceType, S.Generator.Element.Generator.Element: AnyObject>(objects: S?) -> [[Self]] {
return Coder.decode(objects)
static func decode<S: Sequence>(_ objects: S?) -> [[Self]] where S.Iterator.Element: Sequence, S.Iterator.Element.Iterator.Element: AnyObject {
return objects?.flatMap(Self.decode) ?? []
}

/**
Expand All @@ -134,15 +113,15 @@ extension ValueCoding where Coder: NSCoding, Coder.ValueType == Self {
encoder.encodeObject(foo.encoded, forKey: "foo")

*/
public var encoded: Coder {
var encoded: Coder {
return Coder(self)
}
}

extension SequenceType where
Generator.Element: ValueCoding,
Generator.Element.Coder: NSCoding,
Generator.Element.Coder.ValueType == Generator.Element {
extension Sequence where
Iterator.Element: ValueCoding,
Iterator.Element.Coder: NSCoding,
Iterator.Element.Coder.Value == Iterator.Element {

/**
Encodes the sequence of value types into an array of coders.
Expand All @@ -155,22 +134,22 @@ extension SequenceType where
encoder.encodeObject(foos.encoded, forKey: "foos")

*/
public var encoded: [Generator.Element.Coder] {
public var encoded: [Iterator.Element.Coder] {
return map { $0.encoded }
}
}

extension SequenceType where
Generator.Element: SequenceType,
Generator.Element.Generator.Element: ValueCoding,
Generator.Element.Generator.Element.Coder: NSCoding,
Generator.Element.Generator.Element.Coder.ValueType == Generator.Element.Generator.Element {
extension Sequence where
Iterator.Element: Sequence,
Iterator.Element.Iterator.Element: ValueCoding,
Iterator.Element.Iterator.Element.Coder: NSCoding,
Iterator.Element.Iterator.Element.Coder.Value == Iterator.Element.Iterator.Element {

/**
Encodes a sequence of sequences of value types into
an array of arrays of coders.
*/
public var encoded: [[Generator.Element.Generator.Element.Coder]] {
public var encoded: [[Iterator.Element.Iterator.Element.Coder]] {
return map { $0.encoded }
}
}
2 changes: 1 addition & 1 deletion Supporting Files/ValueCoding.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ APPLICATION_EXTENSION_API_ONLY_xctest = NO
APPLICATION_EXTENSION_API_ONLY = $(APPLICATION_EXTENSION_API_ONLY_$(WRAPPER_EXTENSION))

// Build Settings
SWIFT_VERSION = 2.3
SWIFT_VERSION = 3.0
SUPPORTED_PLATFORMS = macosx iphoneos appletvos watchos appletvsimulator iphonesimulator watchsimulator
CLANG_ENABLE_CODE_COVERAGE = YES

Expand Down
2 changes: 1 addition & 1 deletion Supporting Files/Version.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VALUECODING_VERSION = 1.5.0
VALUECODING_VERSION = 2.0.0
41 changes: 37 additions & 4 deletions Tests/Support.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Foo: ValueCoding {
let bar: String
}

class FooCoder: NSObject, NSCoding, CodingType {
class FooCoder: NSObject, NSCoding, CodingProtocol {

enum Keys: String {
case Bar = "bar"
Expand All @@ -27,12 +27,12 @@ class FooCoder: NSObject, NSCoding, CodingType {
}

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

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

Expand All @@ -41,3 +41,36 @@ extension Foo: Equatable { }
func == (lhs: Foo, rhs: Foo) -> Bool {
return lhs.bar == rhs.bar
}

struct Baz: ValueCoding {
typealias Coder = BazCoder
let bat: String
}

extension Baz: Equatable { }

func == (lhs: Baz, rhs: Baz) -> Bool {
return lhs.bat == rhs.bat
}

class BazCoder: NSObject, NSCoding, CodingProtocol {

enum Keys: String {
case Bat = "bat"
}

let value: Baz

required init(_ aValue: Baz) {
value = aValue
}

required init?(coder aDecoder: NSCoder) {
let bat = aDecoder.decodeObject(forKey: Keys.Bat.rawValue) as? String
value = Baz(bat: bat!)
}

func encode(with aCoder: NSCoder) {
aCoder.encode(value.bat, forKey: Keys.Bat.rawValue)
}
}
11 changes: 8 additions & 3 deletions Tests/ValueCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class ValueCodingTests: XCTestCase {
XCTAssertEqual(unarchived!, item)
}

func test__single_incorrect_archiving() {
let unarchived = Baz.decode(item.encoded)
XCTAssertNil(unarchived)
}

func test__multiple_archiving() {
let unarchived = Foo.decode(items.encoded)
XCTAssertEqual(unarchived, items)
Expand All @@ -61,17 +66,17 @@ class ValueCodingTests: XCTestCase {
}

func test__with_single_nil() {
let empty: AnyObject? = .None
let empty: AnyObject? = .none
XCTAssertNil(Foo.decode(empty))
}

func test__with_sequence_nil() {
let empty: [AnyObject]? = .None
let empty: [AnyObject]? = .none
XCTAssertTrue(Foo.decode(empty).isEmpty)
}

func test__with_nested_nil() {
let empty: [[AnyObject]]? = .None
let empty: [[AnyObject]]? = .none
XCTAssertTrue(Foo.decode(empty).isEmpty)
}

Expand Down
3 changes: 1 addition & 2 deletions ValueCoding.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ValueCoding"
s.version = "1.5.0"
s.version = "2.0.0"
s.summary = "Swift protocols for encoding/decoding value types."
s.description = <<-DESC
Expand All @@ -19,7 +19,6 @@ Pod::Spec.new do |s|
s.author = { "Daniel Thorpe" => "@danthorpe" }
s.source = { :git => "https://github.com/danthorpe/ValueCoding.git", :tag => s.version.to_s }
s.module_name = 'ValueCoding'
s.documentation_url = 'http://docs.danthorpe.me/valuecoding/1.5.0/index.html'
s.social_media_url = 'https://twitter.com/danthorpe'
s.requires_arc = true
s.ios.deployment_target = '8.0'
Expand Down
2 changes: 2 additions & 0 deletions ValueCoding.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@
TargetAttributes = {
658A7B5B1D776B7600F897C8 = {
CreatedOnToolsVersion = 8.0;
LastSwiftMigration = 0800;
ProvisioningStyle = Automatic;
};
658A7B641D776B7600F897C8 = {
CreatedOnToolsVersion = 8.0;
LastSwiftMigration = 0800;
ProvisioningStyle = Automatic;
};
};
Expand Down

0 comments on commit b8e67db

Please sign in to comment.