Skip to content

Commit

Permalink
Fix for dictionary to AnyObject
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Dec 22, 2016
1 parent 762b73a commit 84d51b2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 13 deletions.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "EVReflection"
s.version = "3.5.0"
s.version = "3.5.1"
s.summary = "iOS: Swift helper library with reflection functions"
s.description = "Swift helper library with reflection functions including support for NSCoding, Printable, Hashable, Equatable and JSON"
s.homepage = "https://github.com/evermeer/EVReflection"
Expand Down
20 changes: 10 additions & 10 deletions EVReflection/EVReflection.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
7FCD1D491D3FADA800F51BE0 /* EVReflectionIssue107.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FCD1D471D3FAD0900F51BE0 /* EVReflectionIssue107.swift */; };
7FCD1D4A1D3FADA900F51BE0 /* EVReflectionIssue107.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FCD1D471D3FAD0900F51BE0 /* EVReflectionIssue107.swift */; };
7FCD1D4B1D3FADAB00F51BE0 /* EVReflectionIssue107.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FCD1D471D3FAD0900F51BE0 /* EVReflectionIssue107.swift */; };
7FD407AC1E0BF78100AEBF1B /* EVReflectionIssue145.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FD407AA1E0BE8BE00AEBF1B /* EVReflectionIssue145.swift */; };
7FD407AD1E0BF78200AEBF1B /* EVReflectionIssue145.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FD407AA1E0BE8BE00AEBF1B /* EVReflectionIssue145.swift */; };
7FD407AE1E0BF78300AEBF1B /* EVReflectionIssue145.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FD407AA1E0BE8BE00AEBF1B /* EVReflectionIssue145.swift */; };
7FD41E0A1CDF441400746FB8 /* EVObjectDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F3E19291B8526CA00A6E6C6 /* EVObjectDescription.swift */; };
7FD41E0B1CDF441600746FB8 /* EVObjectDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F3E19291B8526CA00A6E6C6 /* EVObjectDescription.swift */; };
7FD41E0C1CDF441800746FB8 /* EVObjectDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F3E19291B8526CA00A6E6C6 /* EVObjectDescription.swift */; };
Expand Down Expand Up @@ -260,6 +263,7 @@
7F9E68111C67728200F0F4D3 /* EVWorkaroundHelpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVWorkaroundHelpers.swift; sourceTree = "<group>"; };
7FB099E11D2F82DE00D94204 /* EVReflectionIssue102.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue102.swift; sourceTree = "<group>"; };
7FCD1D471D3FAD0900F51BE0 /* EVReflectionIssue107.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue107.swift; sourceTree = "<group>"; };
7FD407AA1E0BE8BE00AEBF1B /* EVReflectionIssue145.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue145.swift; sourceTree = "<group>"; };
7FD67D801C28463E0071234B /* EVReflection OSX Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "EVReflection OSX Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
7FD67D811C284A2C0071234B /* EVReflection TVOS Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "EVReflection TVOS Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
7FD67D821C2882320071234B /* EVReflection.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = EVReflection.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -507,6 +511,7 @@
7F70E80C1D96BF6A0020DB2C /* EVReflectionIssueAF30.swift */,
7F7663371DA922A300740ACA /* EVReflectionIssue114b.swift */,
7FE271641DDDCF8400C8B4CD /* EVReflectionIssue141.swift */,
7FD407AA1E0BE8BE00AEBF1B /* EVReflectionIssue145.swift */,
);
name = GitHubIssues;
sourceTree = "<group>";
Expand Down Expand Up @@ -899,6 +904,7 @@
files = (
7F7663391DA922FB00740ACA /* EVReflectionIssue114b.swift in Sources */,
7F41ECB21B91F306003A2236 /* EVReflectionWorkaroundsTests.swift in Sources */,
7FD407AC1E0BF78100AEBF1B /* EVReflectionIssue145.swift in Sources */,
7FD41E0A1CDF441400746FB8 /* EVObjectDescription.swift in Sources */,
7F057CF01D4A851300F3053E /* EVReflectionIssue110.swift in Sources */,
7F59F9DD1C079A7300C14CF9 /* EVReflectionNumbersTests.swift in Sources */,
Expand Down Expand Up @@ -938,6 +944,7 @@
files = (
7F76633A1DA922FB00740ACA /* EVReflectionIssue114b.swift in Sources */,
7F3E31961BB909A7001E6788 /* EVReflectionWorkaroundsTests.swift in Sources */,
7FD407AD1E0BF78200AEBF1B /* EVReflectionIssue145.swift in Sources */,
7FD41E0B1CDF441600746FB8 /* EVObjectDescription.swift in Sources */,
7F057CF11D4A851400F3053E /* EVReflectionIssue110.swift in Sources */,
7F3E31971BB909A7001E6788 /* EVReflectionWorkaroundSwiftGenericsTests.swift in Sources */,
Expand Down Expand Up @@ -999,6 +1006,7 @@
files = (
7F76633B1DA922FC00740ACA /* EVReflectionIssue114b.swift in Sources */,
7F9336CC1C2822B700AF7076 /* EVObjectDescriptionTests.swift in Sources */,
7FD407AE1E0BF78300AEBF1B /* EVReflectionIssue145.swift in Sources */,
7FD41E0C1CDF441800746FB8 /* EVObjectDescription.swift in Sources */,
7F057CF21D4A851400F3053E /* EVReflectionIssue110.swift in Sources */,
7F9336CA1C2822B700AF7076 /* EVReflectionTests.swift in Sources */,
Expand Down Expand Up @@ -1123,7 +1131,6 @@
PRODUCT_BUNDLE_IDENTIFIER = evermeer.EVReflectioniOS;
PRODUCT_NAME = EVReflection;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0.1;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -1146,7 +1153,6 @@
PRODUCT_BUNDLE_IDENTIFIER = evermeer.EVReflectioniOS;
PRODUCT_NAME = EVReflection;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0.1;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down Expand Up @@ -1249,7 +1255,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0.1;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -1292,7 +1298,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0.1;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand All @@ -1310,7 +1316,6 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "nl.evict.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0.1;
};
name = Debug;
};
Expand All @@ -1322,7 +1327,6 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "nl.evict.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0.1;
};
name = Release;
};
Expand Down Expand Up @@ -1401,7 +1405,6 @@
PRODUCT_NAME = EVReflection;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0.1;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -1426,7 +1429,6 @@
PRODUCT_NAME = EVReflection;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0.1;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -1443,7 +1445,6 @@
PRODUCT_BUNDLE_IDENTIFIER = "nl.evict.EVReflection-TVOS-Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = appletvos;
SWIFT_VERSION = 3.0.1;
TVOS_DEPLOYMENT_TARGET = 9.0;
};
name = Debug;
Expand All @@ -1456,7 +1457,6 @@
PRODUCT_BUNDLE_IDENTIFIER = "nl.evict.EVReflection-TVOS-Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = appletvos;
SWIFT_VERSION = 3.0.1;
TVOS_DEPLOYMENT_TARGET = 9.0;
};
name = Release;
Expand Down
1 change: 1 addition & 0 deletions EVReflection/EVReflectionTests/EVReflectionIssue124.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TestIssue124: XCTestCase {
}
}


class Wrapper: EVObject {
var data: [MoreSection]?
}
Expand Down
93 changes: 93 additions & 0 deletions EVReflection/EVReflectionTests/EVReflectionIssue145.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// EVReflectionIssue145.swift
// EVReflection
//
// Created by Edwin Vermeer on 22/12/2016.
// Copyright © 2016 evict. All rights reserved.
//


import Foundation
import XCTest

@testable import EVReflection


class TestIssue145: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
EVReflection.setBundleIdentifier(User.self)
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testIssue145() {
let jsonDictOriginal: NSDictionary = [
"id": 24,
"name": "John Appleseed",
"email": "[email protected]",
"company": [
"name": "Apple",
"address": "1 Infinite Loop, Cupertino, CA"
],
"workHistory": [
["name": "Google", "address": "1600 Amphitheatre Parkway, Mauntain View"],
["name": "Facebook", "address": "1 Hacker Way, Menlo Park"]
]
]
print("Initial dictionary:\n\(jsonDictOriginal)\n\n")

let userOriginal = User145a(dictionary: jsonDictOriginal)
print("object description: \n\(userOriginal)\n\n")

let jsonString = userOriginal.toJsonString()
print("JSON string from dictionary: \n\(jsonString)\n\n")

let userDictionary = User145b(json: jsonString)
print("object description with NSDictionary: \n\(userDictionary)\n\n")

let userAnyObject = User145c(json: jsonString)
print("object description with AnyObject: \n\(userAnyObject)\n\n")

let finalJson = userAnyObject.toJsonString()
print("Final JSON string from object with AnyObject: \n\(jsonString)\n\n")
XCTAssertEqual(jsonString, finalJson, "Json should be the same")

XCTAssert(userOriginal.workHistory?.count == 2)
}

}

class User145a: EVObject {
var id: Int = 0
var name: String = ""
var email: String?
var company: Company145?
var workHistory: [Company145]?
}

class User145b: EVObject {
var id: Int = 0
var name: String = ""
var email: String?
var company: NSDictionary?
var workHistory: [NSDictionary]?
}

class User145c: EVObject {
var id: Int = 0
var name: String = ""
var email: String?
var company: AnyObject = NSDictionary()
var workHistory: [AnyObject] = []
}

class Company145: EVObject {
var name: String = ""
var address: String?
}
7 changes: 5 additions & 2 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ final public class EVReflection {
print("ERROR: Invalid json! \(error.localizedDescription)")
}
}

return result
}

Expand Down Expand Up @@ -862,6 +863,8 @@ final public class EVReflection {
}
value = date as AnyObject
}
} else if typeInObject == "AnyObject" {

}

if !(value is NSArray) && (typeInObject ?? "").contains("Swift.Array") {
Expand Down Expand Up @@ -1094,11 +1097,11 @@ final public class EVReflection {
}
} else if let _ = type.range(of: "_NativeDictionaryStorageOwner"), let dict = dictValue as? NSDictionary, let org = anyObject as? EVReflectable {
dictValue = org.convertDictionary(key, dict: dict)
} else if type != "NSDictionary" && dictValue as? NSDictionary != nil { //TODO this too? && original is NSObject
} else if type != "NSDictionary" && type != "__NSDictionary0" && type != "AnyObject" && dictValue as? NSDictionary != nil { //TODO this too? && original is NSObject
let (dict, isValid) = dictToObject(type, original: original as? NSObject, dict: dictValue as? NSDictionary ?? NSDictionary(), conversionOptions: conversionOptions)
dictValue = dict ?? dictValue
valid = isValid
} else if type.range(of: "<NSDictionary>") == nil && dictValue as? [NSDictionary] != nil {
} else if type.range(of: "<NSDictionary>") == nil && type.range(of: "<AnyObject>") == nil && dictValue as? [NSDictionary] != nil {
// Array of objects
dictValue = dictArrayToObjectArray(anyObject, key: key, type: type, array: dictValue as? [NSDictionary] as NSArray? ?? [NSDictionary]() as NSArray, conversionOptions: conversionOptions) as NSArray
} else if dictValue is String && original is NSObject && original is EVReflectable {
Expand Down

0 comments on commit 84d51b2

Please sign in to comment.