From f56cb4cfc62d032602dbd48d0efd192f6cf82b5a Mon Sep 17 00:00:00 2001 From: Baris Sencan Date: Mon, 13 Jun 2016 04:52:37 -0700 Subject: [PATCH] <-- no longer throws --- JSONHelper.podspec | 4 +- JSONHelper/Conversion.swift | 70 +++++++++++++--------- JSONHelper/Deserialization.swift | 50 ++++++++-------- JSONHelperTests/ArrayTests.swift | 12 ++-- JSONHelperTests/BoolTests.swift | 6 +- JSONHelperTests/ColorTests.swift | 2 +- JSONHelperTests/DeserializableTests.swift | 8 +-- JSONHelperTests/DictionaryTests.swift | 12 ++-- JSONHelperTests/DoubleTests.swift | 12 ++-- JSONHelperTests/EnumTests.swift | 2 +- JSONHelperTests/FloatTests.swift | 12 ++-- JSONHelperTests/IntTests.swift | 12 ++-- JSONHelperTests/NSDateTests.swift | 4 +- JSONHelperTests/NSDecimalNumberTests.swift | 12 ++-- JSONHelperTests/NSURLTests.swift | 2 +- JSONHelperTests/StringTests.swift | 6 +- README.md | 44 ++++++-------- 17 files changed, 138 insertions(+), 132 deletions(-) diff --git a/JSONHelper.podspec b/JSONHelper.podspec index 8347b22..7e1816c 100644 --- a/JSONHelper.podspec +++ b/JSONHelper.podspec @@ -1,8 +1,8 @@ Pod::Spec.new do |s| s.name = 'JSONHelper' - s.version = '2.0.0' + s.version = '2.1.0' s.license = { :type => 'zlib', :file => 'LICENSE' } - s.summary = 'Lightning fast JSON deserialization and value conversion library for iOS, tvOS, and OS X written in Swift.' + s.summary = 'Convert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of!' s.homepage = 'https://github.com/isair/JSONHelper' s.author = { 'Baris Sencan' => 'baris.sncn@gmail.com' } diff --git a/JSONHelper/Conversion.swift b/JSONHelper/Conversion.swift index b40bbce..a8c256f 100644 --- a/JSONHelper/Conversion.swift +++ b/JSONHelper/Conversion.swift @@ -26,7 +26,7 @@ public protocol Convertible { // MARK: - Basic Conversion -public func <-- (inout lhs: T?, rhs: U?) throws -> T? { +public func <-- (inout lhs: T?, rhs: U?) -> T? { if !(lhs is NSNull) { lhs = JSONHelper.convertToNilIfNull(rhs) as? T } else { @@ -35,28 +35,41 @@ public func <-- (inout lhs: T?, rhs: U?) throws -> T? { return lhs } -public func <-- (inout lhs: T, rhs: U?) throws -> T { +public func <-- (inout lhs: T, rhs: U?) -> T { var newValue: T? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } -public func <-- (inout lhs: C?, rhs: T?) throws -> C? { - lhs = try C.convertFromValue(JSONHelper.convertToNilIfNull(rhs)) +public func <-- (inout lhs: C?, rhs: T?) -> C? { + lhs = nil + + do { + lhs = try C.convertFromValue(JSONHelper.convertToNilIfNull(rhs)) + } catch ConversionError.InvalidValue { +#if DEBUG + print("Invalid value \(rhs.debugDescription) for supported type.") +#endif + } catch ConversionError.UnsupportedType { +#if DEBUG + print("Unsupported type.") +#endif + } catch {} + return lhs } -public func <-- (inout lhs: C, rhs: T?) throws -> C { +public func <-- (inout lhs: C, rhs: T?) -> C { var newValue: C? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } // MARK: - Array Conversion -public func <-- (inout lhs: [C]?, rhs: [T]?) throws -> [C]? { +public func <-- (inout lhs: [C]?, rhs: [T]?) -> [C]? { guard let rhs = rhs else { lhs = nil return lhs @@ -65,7 +78,8 @@ public func <-- (inout lhs: [C]?, rhs: [T]?) throws -> [C]? { lhs = [C]() for element in rhs { var convertedElement: C? - try convertedElement <-- element + convertedElement <-- element + if let convertedElement = convertedElement { lhs?.append(convertedElement) } @@ -74,36 +88,36 @@ public func <-- (inout lhs: [C]?, rhs: [T]?) throws -> [C]? { return lhs } -public func <-- (inout lhs: [C], rhs: [T]?) throws -> [C] { +public func <-- (inout lhs: [C], rhs: [T]?) -> [C] { var newValue: [C]? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } -public func <-- (inout lhs: [C]?, rhs: T?) throws -> [C]? { +public func <-- (inout lhs: [C]?, rhs: T?) -> [C]? { guard let rhs = rhs else { lhs = nil return lhs } if let elements = rhs as? NSArray as? [AnyObject] { - return try lhs <-- elements + return lhs <-- elements } - throw ConversionError.UnsupportedType + return nil } -public func <-- (inout lhs: [C], rhs: T?) throws -> [C] { +public func <-- (inout lhs: [C], rhs: T?) -> [C] { var newValue: [C]? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } // MARK: - Dictionary Conversion -public func <-- (inout lhs: [T : C]?, rhs: [T : U]?) throws -> [T : C]? { +public func <-- (inout lhs: [T : C]?, rhs: [T : U]?) -> [T : C]? { guard let rhs = rhs else { lhs = nil return lhs @@ -112,7 +126,7 @@ public func <-- (inout lhs: [T : C]?, rhs: [T : U]?) throw lhs = [T : C]() for (key, value) in rhs { var convertedValue: C? - try convertedValue <-- value + convertedValue <-- value if let convertedValue = convertedValue { lhs?[key] = convertedValue } @@ -121,36 +135,36 @@ public func <-- (inout lhs: [T : C]?, rhs: [T : U]?) throw return lhs } -public func <-- (inout lhs: [T : C], rhs: [T : U]?) throws -> [T : C] { +public func <-- (inout lhs: [T : C], rhs: [T : U]?) -> [T : C] { var newValue: [T : C]? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } -public func <-- (inout lhs: [T : C]?, rhs: U?) throws -> [T : C]? { +public func <-- (inout lhs: [T : C]?, rhs: U?) -> [T : C]? { guard let rhs = rhs else { lhs = nil return lhs } if let elements = rhs as? NSDictionary as? [T : AnyObject] { - return try lhs <-- elements + return lhs <-- elements } - throw ConversionError.UnsupportedType + return nil } -public func <-- (inout lhs: [T : C], rhs: U?) throws -> [T : C] { +public func <-- (inout lhs: [T : C], rhs: U?) -> [T : C] { var newValue: [T : C]? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } // MARK: - Enum Conversion -public func <-- (inout lhs: T?, rhs: U?) throws -> T? { +public func <-- (inout lhs: T?, rhs: U?) -> T? { var newValue: T? if let @@ -163,9 +177,9 @@ public func <-- (inout lhs: T?, rhs: U?) throws -> T? { return lhs } -public func <-- (inout lhs: T, rhs: U?) throws -> T { +public func <-- (inout lhs: T, rhs: U?) -> T { var newValue: T? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } diff --git a/JSONHelper/Deserialization.swift b/JSONHelper/Deserialization.swift index 9fb7646..b97cfd4 100644 --- a/JSONHelper/Deserialization.swift +++ b/JSONHelper/Deserialization.swift @@ -8,7 +8,7 @@ import Foundation public protocol Deserializable { /// TODOC - init(dictionary: [String : AnyObject]) throws + init(dictionary: [String : AnyObject]) } // MARK: - Helper Methods @@ -24,13 +24,13 @@ private func dataStringToObject(dataString: String) -> AnyObject? { // MARK: - Basic Deserialization -public func <-- (inout lhs: D?, rhs: T?) throws -> D? { +public func <-- (inout lhs: D?, rhs: T?) -> D? { let cleanedValue = JSONHelper.convertToNilIfNull(rhs) if let jsonObject = cleanedValue as? NSDictionary as? [String : AnyObject] { - lhs = try D(dictionary: jsonObject) + lhs = D(dictionary: jsonObject) } else if let string = cleanedValue as? String { - try lhs <-- dataStringToObject(string) + lhs <-- dataStringToObject(string) } else { lhs = nil } @@ -38,22 +38,23 @@ public func <-- (inout lhs: D?, rhs: T?) throws -> D? { return lhs } -public func <-- (inout lhs: D, rhs: T?) throws -> D { +public func <-- (inout lhs: D, rhs: T?) -> D { var newValue: D? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } // MARK: - Array Deserialization -public func <-- (inout lhs: [D]?, rhs: [T]?) throws -> [D]? { +public func <-- (inout lhs: [D]?, rhs: [T]?) -> [D]? { guard let rhs = rhs else { return nil } lhs = [D]() for element in rhs { var convertedElement: D? - try convertedElement <-- element + convertedElement <-- element + if let convertedElement = convertedElement { lhs?.append(convertedElement) } @@ -62,33 +63,33 @@ public func <-- (inout lhs: [D]?, rhs: [T]?) throws -> [D] return lhs } -public func <-- (inout lhs: [D], rhs: [T]?) throws -> [D] { +public func <-- (inout lhs: [D], rhs: [T]?) -> [D] { var newValue: [D]? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } -public func <-- (inout lhs: [D]?, rhs: T?) throws -> [D]? { +public func <-- (inout lhs: [D]?, rhs: T?) -> [D]? { guard let rhs = rhs else { return nil } if let elements = rhs as? NSArray as? [AnyObject] { - return try lhs <-- elements + return lhs <-- elements } - throw ConversionError.UnsupportedType + return nil } -public func <-- (inout lhs: [D], rhs: T?) throws -> [D] { +public func <-- (inout lhs: [D], rhs: T?) -> [D] { var newValue: [D]? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } // MARK: - Dictionary Deserialization -public func <-- (inout lhs: [T : D]?, rhs: [T : U]?) throws -> [T : D]? { +public func <-- (inout lhs: [T : D]?, rhs: [T : U]?) -> [T : D]? { guard let rhs = rhs else { lhs = nil return lhs @@ -97,7 +98,8 @@ public func <-- (inout lhs: [T : D]?, rhs: [T : U]?) th lhs = [T : D]() for (key, value) in rhs { var convertedValue: D? - try convertedValue <-- value + convertedValue <-- value + if let convertedValue = convertedValue { lhs?[key] = convertedValue } @@ -106,29 +108,29 @@ public func <-- (inout lhs: [T : D]?, rhs: [T : U]?) th return lhs } -public func <-- (inout lhs: [T : D], rhs: [T : U]?) throws -> [T : D] { +public func <-- (inout lhs: [T : D], rhs: [T : U]?) -> [T : D] { var newValue: [T : D]? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } -public func <-- (inout lhs: [T : D]?, rhs: U?) throws -> [T : D]? { +public func <-- (inout lhs: [T : D]?, rhs: U?) -> [T : D]? { guard let rhs = rhs else { lhs = nil return lhs } if let elements = rhs as? NSDictionary as? [T : AnyObject] { - return try lhs <-- elements + return lhs <-- elements } - throw ConversionError.UnsupportedType + return nil } -public func <-- (inout lhs: [T : D], rhs: U?) throws -> [T : D] { +public func <-- (inout lhs: [T : D], rhs: U?) -> [T : D] { var newValue: [T : D]? - try newValue <-- rhs + newValue <-- rhs lhs = newValue ?? lhs return lhs } diff --git a/JSONHelperTests/ArrayTests.swift b/JSONHelperTests/ArrayTests.swift index 4334098..a7de376 100644 --- a/JSONHelperTests/ArrayTests.swift +++ b/JSONHelperTests/ArrayTests.swift @@ -15,7 +15,7 @@ class ArrayTests: XCTestCase { func testArrayToConvertibleArray() { var urls = [NSURL]() - try! urls <-- urlStrings + urls <-- urlStrings XCTAssertEqual(urls[0].host, urlHosts[0]) XCTAssertEqual(urls[1].host, urlHosts[1]) @@ -24,7 +24,7 @@ class ArrayTests: XCTestCase { func testArrayAsAnyToConvertibleArray() { var urls = [NSURL]() - try! urls <-- (urlStrings as Any) + urls <-- (urlStrings as Any) XCTAssertEqual(urls[0].host, urlHosts[0]) XCTAssertEqual(urls[1].host, urlHosts[1]) @@ -38,8 +38,8 @@ class ArrayTests: XCTestCase { var name: String? - init(dictionary: [String: AnyObject]) throws { - try name <-- dictionary[Item.nameKey] + init(dictionary: [String: AnyObject]) { + name <-- dictionary[Item.nameKey] } } @@ -50,14 +50,14 @@ class ArrayTests: XCTestCase { func testArrayToDeserializableArray() { var items = [Item]() - try! items <-- dictionaries + items <-- dictionaries XCTAssertEqual(items[0].name, "a") XCTAssertEqual(items[1].name, "b") } func testArrayAsAnyToDeserializableArray() { var items = [Item]() - try! items <-- (dictionaries as Any) + items <-- (dictionaries as Any) XCTAssertEqual(items[0].name, "a") XCTAssertEqual(items[1].name, "b") } diff --git a/JSONHelperTests/BoolTests.swift b/JSONHelperTests/BoolTests.swift index 3d8d6f7..4a3caea 100644 --- a/JSONHelperTests/BoolTests.swift +++ b/JSONHelperTests/BoolTests.swift @@ -18,20 +18,20 @@ class BoolTests: XCTestCase { } func testBoolConversion() { - try! value <-- (testBool as Any) + value <-- (testBool as Any) XCTAssertEqual(value, testBool) } func testIntConversion() { for intAndResult in testIntsAndResults { - try! value <-- (intAndResult.0 as Any) + value <-- (intAndResult.0 as Any) XCTAssertEqual(value, intAndResult.1) } } func testStringConversion() { for stringAndResult in testStringsAndResults { - try! value <-- (stringAndResult.0 as Any) + value <-- (stringAndResult.0 as Any) XCTAssertEqual(value, stringAndResult.1) } } diff --git a/JSONHelperTests/ColorTests.swift b/JSONHelperTests/ColorTests.swift index c8eafca..f2abbfb 100644 --- a/JSONHelperTests/ColorTests.swift +++ b/JSONHelperTests/ColorTests.swift @@ -25,7 +25,7 @@ class ColorTests: XCTestCase { } func testStringConversion() { - try! value <-- (testStringAndResult.0 as Any) + value <-- (testStringAndResult.0 as Any) var r: CGFloat = 0 var g: CGFloat = 0 diff --git a/JSONHelperTests/DeserializableTests.swift b/JSONHelperTests/DeserializableTests.swift index 31c9c50..906d6e1 100644 --- a/JSONHelperTests/DeserializableTests.swift +++ b/JSONHelperTests/DeserializableTests.swift @@ -13,8 +13,8 @@ class DeserializableTests: XCTestCase { var name = "" - init(dictionary: [String : AnyObject]) throws { - try name <-- dictionary[Item.nameKey] + init(dictionary: [String : AnyObject]) { + name <-- dictionary[Item.nameKey] } init() {} @@ -30,12 +30,12 @@ class DeserializableTests: XCTestCase { } func testDictionaryDeserialization() { - try! item <-- itemDictionary + item <-- itemDictionary XCTAssertEqual(item.name, itemDictionary[Item.nameKey]) } func testStringDeserialization() { - try! item <-- itemString + item <-- itemString XCTAssertEqual(item.name, itemDictionary[Item.nameKey]) } } diff --git a/JSONHelperTests/DictionaryTests.swift b/JSONHelperTests/DictionaryTests.swift index f49c6b8..a9858fe 100644 --- a/JSONHelperTests/DictionaryTests.swift +++ b/JSONHelperTests/DictionaryTests.swift @@ -18,13 +18,13 @@ class DictionaryTests: XCTestCase { func testDictionaryToConvertibleDictionary() { var value = [String : NSDate]() - try! value <-- dateStringDictionary + value <-- dateStringDictionary XCTAssertEqual(JSONHelper.dateFormatter.stringFromDate(value["one"]!), dateStringDictionary["one"]) } func testDictionaryAsAnyToConvertibleDictionary() { var value = [String : NSDate]() - try! value <-- (dateStringDictionary as Any) + value <-- (dateStringDictionary as Any) XCTAssertEqual(JSONHelper.dateFormatter.stringFromDate(value["one"]!), dateStringDictionary["one"]) } @@ -40,21 +40,21 @@ class DictionaryTests: XCTestCase { var name = "" - init(dictionary: [String : AnyObject]) throws { - try name <-- dictionary["name"] + init(dictionary: [String : AnyObject]) { + name <-- dictionary["name"] } } func testDictionaryToMappableDictionary() { var value = [String : Item]() - try! value <-- dictionary + value <-- dictionary XCTAssertEqual(value["one"]?.name, dictionary["one"]?["name"]) XCTAssertEqual(value["two"]?.name, dictionary["two"]?["name"]) } func testDictionaryAsAnyToMappableDictionary() { var value = [String : Item]() - try! value <-- (dictionary as Any) + value <-- (dictionary as Any) XCTAssertEqual(value["one"]?.name, dictionary["one"]?["name"]) XCTAssertEqual(value["two"]?.name, dictionary["two"]?["name"]) } diff --git a/JSONHelperTests/DoubleTests.swift b/JSONHelperTests/DoubleTests.swift index 385b230..a0c3347 100644 --- a/JSONHelperTests/DoubleTests.swift +++ b/JSONHelperTests/DoubleTests.swift @@ -21,32 +21,32 @@ class DoubleTests: XCTestCase { } func testIntConversion() { - try! value <-- (testInt as Any) + value <-- (testInt as Any) XCTAssert(Int(value) == testInt) } func testFloatConversion() { - try! value <-- (testFloat as Any) + value <-- (testFloat as Any) XCTAssertEqual(value, testDouble) } func testDoubleConversion() { - try! value <-- (testDouble as Any) + value <-- (testDouble as Any) XCTAssertEqual(value, testDouble) } func testNSNumberConversion() { - try! value <-- (testNSNumber as Any) + value <-- (testNSNumber as Any) XCTAssertEqual(value, testDouble) } func testNSDecimalNumberConversion() { - try! value <-- (testNSDecimalNumber as Any) + value <-- (testNSDecimalNumber as Any) XCTAssertEqual(value, testDouble) } func testStringConversion() { - try! value <-- (testString as Any) + value <-- (testString as Any) XCTAssertEqual(value, testDouble) } } diff --git a/JSONHelperTests/EnumTests.swift b/JSONHelperTests/EnumTests.swift index 63f60ac..1fb011a 100644 --- a/JSONHelperTests/EnumTests.swift +++ b/JSONHelperTests/EnumTests.swift @@ -19,7 +19,7 @@ class EnumTests: XCTestCase { } func testStringConversion() { - try! value <-- (TestEnum.Friend.rawValue as Any) + value <-- (TestEnum.Friend.rawValue as Any) XCTAssertEqual(value, TestEnum.Friend) } } diff --git a/JSONHelperTests/FloatTests.swift b/JSONHelperTests/FloatTests.swift index 03546d6..86bb826 100644 --- a/JSONHelperTests/FloatTests.swift +++ b/JSONHelperTests/FloatTests.swift @@ -21,32 +21,32 @@ class FloatTests: XCTestCase { } func testIntConversion() { - try! value <-- (testInt as Any) + value <-- (testInt as Any) XCTAssert(Int(value) == testInt) } func testFloatConversion() { - try! value <-- (testFloat as Any) + value <-- (testFloat as Any) XCTAssertEqual(value, testFloat) } func testDoubleConversion() { - try! value <-- (testDouble as Any) + value <-- (testDouble as Any) XCTAssertEqual(value, testFloat) } func testNSNumberConversion() { - try! value <-- (testNSNumber as Any) + value <-- (testNSNumber as Any) XCTAssertEqual(value, testFloat) } func testNSDecimalNumberConversion() { - try! value <-- (testNSDecimalNumber as Any) + value <-- (testNSDecimalNumber as Any) XCTAssertEqual(value, testFloat) } func testStringConversion() { - try! value <-- (testString as Any) + value <-- (testString as Any) XCTAssertEqual(value, testFloat) } } diff --git a/JSONHelperTests/IntTests.swift b/JSONHelperTests/IntTests.swift index c1f6028..57bfd55 100644 --- a/JSONHelperTests/IntTests.swift +++ b/JSONHelperTests/IntTests.swift @@ -25,32 +25,32 @@ class IntTests: XCTestCase { } func testIntConversion() { - try! value <-- (testInt as Any) + value <-- (testInt as Any) XCTAssertEqual(value, testInt) } func testFloatConversion() { - try! value <-- (testFloat as Any) + value <-- (testFloat as Any) XCTAssertEqual(value, testInt) } func testDoubleConversion() { - try! value <-- (testDouble as Any) + value <-- (testDouble as Any) XCTAssertEqual(value, testInt) } func testNSNumberConversion() { - try! value <-- (testNSNumber as Any) + value <-- (testNSNumber as Any) XCTAssertEqual(value, testInt) } func testNSDecimalNumberConversion() { - try! value <-- (testNSDecimalNumber as Any) + value <-- (testNSDecimalNumber as Any) XCTAssertEqual(value, testInt) } func testStringConversion() { - try! value <-- (testString as Any) + value <-- (testString as Any) XCTAssertEqual(value, testInt) } } diff --git a/JSONHelperTests/NSDateTests.swift b/JSONHelperTests/NSDateTests.swift index f19851c..ff7ab62 100644 --- a/JSONHelperTests/NSDateTests.swift +++ b/JSONHelperTests/NSDateTests.swift @@ -18,13 +18,13 @@ class NSDateTests: XCTestCase { func testEpochTimestampConversion() { var date = NSDate() - try! date <-- (epochTimestamp as Any) + date <-- (epochTimestamp as Any) XCTAssertEqual(JSONHelper.dateFormatter.stringFromDate(date), dateString) } func testStringConversion() { var date = NSDate() - try! date <-- (dateString as Any) + date <-- (dateString as Any) XCTAssertEqual(JSONHelper.dateFormatter.stringFromDate(date), dateString) } } diff --git a/JSONHelperTests/NSDecimalNumberTests.swift b/JSONHelperTests/NSDecimalNumberTests.swift index 046b247..33e60f8 100644 --- a/JSONHelperTests/NSDecimalNumberTests.swift +++ b/JSONHelperTests/NSDecimalNumberTests.swift @@ -21,32 +21,32 @@ class NSDecimalNumberTests: XCTestCase { } func testIntConversion() { - try! value <-- (testInt as Any) + value <-- (testInt as Any) XCTAssert(Int(value) == testInt) } func testFloatConversion() { - try! value <-- (testFloat as Any) + value <-- (testFloat as Any) XCTAssertEqual(value, testNSDecimalNumber) } func testDoubleConversion() { - try! value <-- (testDouble as Any) + value <-- (testDouble as Any) XCTAssertEqual(value, testNSDecimalNumber) } func testNSNumberConversion() { - try! value <-- (testNSNumber as Any) + value <-- (testNSNumber as Any) XCTAssertEqual(value, testNSDecimalNumber) } func testNSDecimalNumberConversion() { - try! value <-- (testNSDecimalNumber as Any) + value <-- (testNSDecimalNumber as Any) XCTAssertEqual(value, testNSDecimalNumber) } func testStringConversion() { - try! value <-- (testString as Any) + value <-- (testString as Any) XCTAssertEqual(value, testNSDecimalNumber) } } diff --git a/JSONHelperTests/NSURLTests.swift b/JSONHelperTests/NSURLTests.swift index 0cfac35..c6e80a2 100644 --- a/JSONHelperTests/NSURLTests.swift +++ b/JSONHelperTests/NSURLTests.swift @@ -16,7 +16,7 @@ class NSURLTests: XCTestCase { func testStringConversion() { var url = NSURL() - try! url <-- (urlString as Any) + url <-- (urlString as Any) XCTAssertEqual(url.host, urlHost) } } diff --git a/JSONHelperTests/StringTests.swift b/JSONHelperTests/StringTests.swift index b0b29a7..7e34cfd 100644 --- a/JSONHelperTests/StringTests.swift +++ b/JSONHelperTests/StringTests.swift @@ -23,12 +23,12 @@ class StringTests: XCTestCase { } func testStringConversion() { - try! value <-- (testString as Any) + value <-- (testString as Any) XCTAssertEqual(value, testString) } func testIntConversion() { - try! value <-- (testIntAndResult.0 as Any) + value <-- (testIntAndResult.0 as Any) XCTAssertEqual(value, testIntAndResult.1) } @@ -36,7 +36,7 @@ class StringTests: XCTestCase { JSONHelper.dateFormatter.dateFormat = testDateFormat JSONHelper.dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) - try! value <-- (testDateAndResult.0 as Any) + value <-- (testDateAndResult.0 as Any) XCTAssertEqual(value, testDateAndResult.1) } } diff --git a/README.md b/README.md index b55356c..3d0f5cf 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![Gratipay](https://img.shields.io/gratipay/bsencan91.svg)](https://gratipay.com/bsencan91/) [![Gitter](https://badges.gitter.im/JOIN CHAT.svg)](https://gitter.im/isair/JSONHelper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -Convert anything into anything in one line; hex strings into UIColor/NSColor, JSON strings into class instances, numbers to strings, etc, anything you can make sense of! +Convert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of! __Latest version requires iOS 8+ and Xcode 7.3+__ @@ -15,11 +15,9 @@ __Latest version requires iOS 8+ and Xcode 7.3+__ 1. [Installation](#installation) 2. [The <-- Operator](#the----operator) - - [Overview](#overview) - - [Convertible Protocol](#convertible-protocol) - - [Deserializable Protocol](#deserializable-protocol) - - [Serializable Protocol](#serializable-protocol) -3. [JSON Deserialization Example](#simple-tutorial) +3. [Convertible Protocol](#convertible-protocol) +4. [Deserializable Protocol](#deserializable-protocol) (with JSON deserialization example) +5. [Serializable Protocol](#serializable-protocol) ## Installation @@ -43,24 +41,16 @@ Then do `carthage update`. After that, add the framework to your project. ## The <-- Operator -### Overview +The `<--` operator takes the value on its right hand side and tries to convert it into the type of the value on its left hand side. If the conversion fails, an error is logged on debug builds. If it's successful, the value of the left hand side variable is overwritten. It's chainable as well. -The `<--` operator takes the value on its right hand side and tries to convert it into the type of the value on its left hand side. If the conversion fails, an error is thrown. If it's successful, the value of the left hand side variable is overwritten. It's chainable as well. - -If the right hand side value is nil, and the left hand side variable is an optional, then nil is assigned to it. When the left hand side is non-optional, in the case where the right hand side value is nil, the current value of the left hand side variable is left untouched. +If the right hand side value is nil or the conversion fails, and the left hand side variable is an optional, then nil is assigned to it. When the left hand side is non-optional, the current value of the left hand side variable is left untouched. Using this specification let's assume you have a dictionary response that you retrieved from some API with hex color strings in it, under the key `colors`, that you want to convert into an array of UIColor instances. Also, to fully use everything we know, let's also assume that we want to have a default value for our color array in case the value for the key we're looking for does not exist (is nil). ```swift -var colors = [UIColor.blackColor()] -// Assume we have response { "colors": ["#fff"] } -try colors <-- response[colorsKey] -``` - -It's really that simple. If you want to ignore any conversion errors instead of having to catch them, you can replace the last line with the following. - -```swift -_ = try? colors <-- response[colorsKey] +var colors = [UIColor.blackColor(), UIColor.whiteColor()] +// Assume we have response { "colors": ["#aaa", "#b06200aa"] } +colors <-- response[colorsKey] ``` ### Convertible Protocol @@ -92,12 +82,12 @@ struct Vector2D: Convertible { ```swift var myVector: Vector2D? -try myVector <-- (1.0, 2.7) +myVector <-- (1.0, 2.7) ``` ### Deserializable Protocol -While you can basically adopt the `Convertible` protocol for any type, if your type is always converted from a dictionary things can get a lot easier with the `Deserializable` protocol. +While you can basically adopt the `Convertible` protocol for any type, if your type is always converted from a dictionary or a JSON string then things can get a lot easier with the `Deserializable` protocol. Example: ```swift @@ -113,19 +103,19 @@ class User: Deserializable { private(set) var avatarURL = NSURL(string: "https://mysite.com/assets/default-avatar.png") required init(dictionary: [String : AnyObject]) { - _ = try? id <-- dictionary[User.idKey] - _ = try? email <-- dictionary[User.emailKey] - _ = try? name <-- dictionary[User.nameKey] - _ = try? avatarURL <-- dictionary[User.avatarURLKey] + id <-- dictionary[User.idKey] + email <-- dictionary[User.emailKey] + name <-- dictionary[User.nameKey] + avatarURL <-- dictionary[User.avatarURLKey] } } ``` ```swift var myUser: User? -try user <-- apiResponse["user"] +user <-- apiResponse["user"] ``` ### Serializable Protocol -// Serialization is coming soon. I'll probably not add a new protocol and just rename and update the Deserializable protocol. +// Serialization is coming soon. I'll probably not add a new protocol and just rename and update the Deserializable protocol and turn it into a mixin.