From b8bf070182055e7cb386bd6c1d862879d5324cfa Mon Sep 17 00:00:00 2001 From: Masher Shin Date: Mon, 29 Aug 2016 14:40:55 +0900 Subject: [PATCH 1/2] mapping to array from string --- .../EVReflectionTests/EVReflectionTests.swift | 24 +++++++++++++++++++ EVReflection/pod/EVReflection.swift | 5 ++++ 2 files changed, 29 insertions(+) diff --git a/EVReflection/EVReflectionTests/EVReflectionTests.swift b/EVReflection/EVReflectionTests/EVReflectionTests.swift index f8879141..377d0a4a 100644 --- a/EVReflection/EVReflectionTests/EVReflectionTests.swift +++ b/EVReflection/EVReflectionTests/EVReflectionTests.swift @@ -353,6 +353,30 @@ class EVReflectionTests: XCTestCase { let b = ImLazy(json: json) print(b) } + + func testArrayFromNotArray() { + let normal = NSDictionary(dictionary: ["bs": [["val": 1], ["val": 2]]]) + print(String(normal)) + let aaNormal: AA = AA(dictionary: normal) + print(String(aaNormal.toDictionary())) + XCTAssertEqual(String(normal), String(aaNormal.toDictionary())) + + let abnormal = NSDictionary(dictionary: ["bs": ["val": 1]]) + print(String(abnormal)) + let aaAbnormal: AA = AA(dictionary: abnormal) + print(String(aaAbnormal.toDictionary())) + XCTAssertEqual(aaAbnormal.bs[0].val, 1) + + let arrayDic = NSDictionary(dictionary: ["strings": ["a", "b"]]) + let arrObj: ArrayObjects = ArrayObjects(dictionary: arrayDic) + print(arrObj) + XCTAssertEqual((arrayDic["strings"] as! NSArray), NSArray(array: arrObj.strings)) + + let arrayDic2 = NSDictionary(dictionary: ["strings": "a"]) + let arrObj2: ArrayObjects = ArrayObjects(dictionary: arrayDic2) + print(arrObj2) + XCTAssertEqual(arrObj2.strings[0], "a") + } } diff --git a/EVReflection/pod/EVReflection.swift b/EVReflection/pod/EVReflection.swift index 504da844..d8b87613 100644 --- a/EVReflection/pod/EVReflection.swift +++ b/EVReflection/pod/EVReflection.swift @@ -754,6 +754,11 @@ final public class EVReflection { value = date } } + + if (value is NSArray) == false && (typeInObject ?? "").lowercaseString.containsString("array") == true { + value = NSArray(array: [value]) + } + if typeInObject == "Struct" { anyObject.setValue(value, forUndefinedKey: key) } else { From 156a6f4e027ebd9b836f2fa5595891d87e11e103 Mon Sep 17 00:00:00 2001 From: Edwin Vermeer Date: Mon, 29 Aug 2016 08:39:53 +0200 Subject: [PATCH 2/2] minor update to pull request no case insensitive compare needed, no compare with true or false needed --- EVReflection/pod/EVReflection.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EVReflection/pod/EVReflection.swift b/EVReflection/pod/EVReflection.swift index d8b87613..a0449c66 100644 --- a/EVReflection/pod/EVReflection.swift +++ b/EVReflection/pod/EVReflection.swift @@ -755,7 +755,7 @@ final public class EVReflection { } } - if (value is NSArray) == false && (typeInObject ?? "").lowercaseString.containsString("array") == true { + if !(value is NSArray) && (typeInObject ?? "").containsString("Array") { value = NSArray(array: [value]) }