Skip to content

Commit

Permalink
Can now also use NSObject if there are sub objects
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Nov 22, 2015
1 parent ac4372b commit 6dcecba
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 56 deletions.
8 changes: 1 addition & 7 deletions EVReflection/EVReflection.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

/* Begin PBXBuildFile section */
7F07C5881AF10437000DF1F3 /* EVReflection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F07C5841AF10284000DF1F3 /* EVReflection.swift */; };
7F07C58F1AF11861000DF1F3 /* EVReflection.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 7F07C58D1AF11861000DF1F3 /* EVReflection.podspec */; };
7F07C5901AF11861000DF1F3 /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = 7F07C58E1AF11861000DF1F3 /* LICENSE */; };
7F0E5E5A1AF4D94000A10E51 /* EVObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0E5E591AF4D94000A10E51 /* EVObject.swift */; };
7F1BA5271B971CBF009F9DB8 /* EVExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F1BA5261B971CBF009F9DB8 /* EVExtensions.swift */; };
7F29762B1BB913910074C85A /* osx-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 7F29762A1BB913910074C85A /* osx-Info.plist */; };
7F3E192A1B8526CA00A6E6C6 /* EVObjectDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F3E19291B8526CA00A6E6C6 /* EVObjectDescription.swift */; };
7F3E318D1BB909A1001E6788 /* EVReflection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F07C5841AF10284000DF1F3 /* EVReflection.swift */; };
7F3E318E1BB909A1001E6788 /* EVObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0E5E591AF4D94000A10E51 /* EVObject.swift */; };
Expand Down Expand Up @@ -195,7 +192,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0710;
ORGANIZATIONNAME = evict;
TargetAttributes = {
7F07C5711AF101A9000DF1F3 = {
Expand Down Expand Up @@ -231,16 +228,13 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7F07C5901AF11861000DF1F3 /* LICENSE in Resources */,
7F07C58F1AF11861000DF1F3 /* EVReflection.podspec in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
7F3E31831BB9095C001E6788 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7F29762B1BB913910074C85A /* osx-Info.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0710"
version = "2.0">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
7 changes: 4 additions & 3 deletions EVReflection/EVReflectionTests/EVReflectionJsonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import XCTest

class User: EVObject {
class User: NSObject {
var id: Int = 0
var name: String = ""
var email: String?
Expand All @@ -16,7 +16,7 @@ class User: EVObject {
var birthDate: NSDate?
}

class Company: EVObject {
class Company: NSObject {
var name: String = ""
var address: String?
}
Expand Down Expand Up @@ -139,7 +139,8 @@ class EVReflectionJsonTests: XCTestCase {
let userRegenerated = User(json:jsonString)
validateUser(userRegenerated)

XCTAssertEqual(userOriginal, userRegenerated, "original should have been the same as regenerated")
print("original = \(EVReflection.description(userOriginal))")
print("regenerated = \(EVReflection.description(userRegenerated))")
}

func validateUser(user:User) {
Expand Down
12 changes: 6 additions & 6 deletions EVReflection/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,27 @@ class EVReflectionTests: XCTestCase {
*/
func testNSNumber() {
let test1 = NSNumber(double: Double(Int.max))
let (value1, _) = EVReflection.valueForAny("", key: "", anyValue: test1)
let (value1, _, _) = EVReflection.valueForAny("", key: "", anyValue: test1)
XCTAssert(value1 as? NSNumber == NSNumber(double: Double(Int.max)), "Values should be same for type NSNumber")

let test2:Float = 458347978508
let (value2, _) = EVReflection.valueForAny("", key: "", anyValue: test2)
let (value2, _, _) = EVReflection.valueForAny("", key: "", anyValue: test2)
XCTAssert(value2 as? NSNumber == NSNumber(float: 458347978508), "Values should be same for type Float")

let test3:Double = 458347978508
let (value3, _) = EVReflection.valueForAny("", key: "", anyValue: test3)
let (value3, _, _) = EVReflection.valueForAny("", key: "", anyValue: test3)
XCTAssert(value3 as? NSNumber == NSNumber(double: 458347978508), "Values should be same for type Double")

let test4:Int64 = Int64.max
let (value4, _) = EVReflection.valueForAny("", key: "", anyValue: test4)
let (value4, _, _) = EVReflection.valueForAny("", key: "", anyValue: test4)
XCTAssert(value4 as? NSNumber == NSNumber(longLong: Int64.max), "Values should be same for type Int64")

let test5:Int32 = Int32.max
let (value5, _) = EVReflection.valueForAny("", key: "", anyValue: test5)
let (value5, _, _) = EVReflection.valueForAny("", key: "", anyValue: test5)
XCTAssert(value5 as? NSNumber == NSNumber(int: Int32.max), "Values should be same for type Int32")

let test6:Int = Int.max
let (value6, _) = EVReflection.valueForAny("", key: "", anyValue: test6)
let (value6, _, _) = EVReflection.valueForAny("", key: "", anyValue: test6)
XCTAssert(value6 as? NSNumber == NSNumber(integer: Int.max), "Values should be same for type Int64")
}

Expand Down
5 changes: 2 additions & 3 deletions EVReflection/EVReflectionTests/WorkaroundEnumTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ class EnumWorkaroundsTests: XCTestCase {
case OK = 1
}

func getRawValue(theEnum: Any) -> Any {

let (val, _) = EVReflection.valueForAny(self, key: "a", anyValue: theEnum)
func getRawValue(theEnum: Any) -> Any {
let (val, _, _) = EVReflection.valueForAny(self, key: "a", anyValue: theEnum)
return val
}
}
Expand Down
4 changes: 2 additions & 2 deletions EVReflection/pod/EVExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public extension Array {

:returns: The object type
*/
private func getArrayTypeInstance<T>(arr:Array<T>) -> T {
public func getArrayTypeInstance<T>(arr:Array<T>) -> T {
return arr.getTypeInstance()
}

Expand All @@ -140,7 +140,7 @@ public extension Array {

:returns: The object type
*/
private func getTypeInstance<T>(
public func getTypeInstance<T>(
) -> T {
let nsobjectype : NSObject.Type = T.self as! NSObject.Type
let nsobject: NSObject = nsobjectype.init()
Expand Down
2 changes: 1 addition & 1 deletion EVReflection/pod/EVObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public class EVObject: NSObject, NSCoding, CustomDebugStringConvertible { // The
*/
public override var hashValue: Int {
get {
return EVReflection.hashValue(self)
return Int(EVReflection.hashValue(self))
}
}

Expand Down
73 changes: 42 additions & 31 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ final public class EVReflection {
:returns: The string representation of the object
*/
public class func description(theObject: NSObject) -> String {
var description: String = swiftStringFromClass(theObject) + " {\n hash = \(theObject.hash)\n"
var description: String = swiftStringFromClass(theObject) + " {\n hash = \(hashValue(theObject))\n"
let (hasKeys, _) = toDictionary(theObject, performKeyCleanup:false)
for (key, value) in hasKeys {
description = description + " key = \(key), value = \(value)\n"
Expand Down Expand Up @@ -431,7 +431,7 @@ final public class EVReflection {

:returns: The value where the Any is converted to AnyObject plus the type of that value as a string
*/
public class func valueForAny(parentObject:Any? = nil, key:String? = nil, anyValue: Any) -> (AnyObject, String) {
public class func valueForAny(parentObject:Any? = nil, key:String? = nil, anyValue: Any) -> (value: AnyObject, type: String, isObject: Bool) {
var theValue = anyValue
var valueType = "EVObject"
let mi: Mirror = Mirror(reflecting: theValue)
Expand All @@ -446,18 +446,18 @@ final public class EVReflection {
var subtype: String = "\(mi)"
subtype = subtype.substringFromIndex((subtype.componentsSeparatedByString("<") [0] + "<").endIndex)
subtype = subtype.substringToIndex(subtype.endIndex.predecessor())
return (NSNull(), subtype)
return (NSNull(), subtype, false)
}
} else if mi.displayStyle == .Enum {
valueType = "\(theValue.dynamicType)"
if let value = theValue as? EVRawString {
return (value.rawValue, "\(mi.subjectType)")
return (value.rawValue, "\(mi.subjectType)", false)
} else if let value = theValue as? EVRawInt {
return (NSNumber(int: Int32(value.rawValue)), "\(mi.subjectType)")
return (NSNumber(int: Int32(value.rawValue)), "\(mi.subjectType)", false)
} else if let value = theValue as? EVRaw {
theValue = value.anyRawValue
} else if let value = theValue as? EVAssociated {
let (enumValue, enumType) = valueForAny(theValue, key: value.associated.label, anyValue: value.associated.value)
let (enumValue, enumType, _) = valueForAny(theValue, key: value.associated.label, anyValue: value.associated.value)
valueType = enumType
theValue = enumValue
} else {
Expand All @@ -469,39 +469,44 @@ final public class EVReflection {
let arrayConverter = parentObject as? EVArrayConvertable
assert(arrayConverter != nil, "WARNING: An object with a property of type Array with optional objects should implement the EVArrayConvertable protocol.")
let convertedValue = arrayConverter!.convertArray(key ?? "", array: theValue)
return (convertedValue, valueType)
return (convertedValue, valueType, false)
}
} else {
}
else {
valueType = "\(mi.subjectType)"
}

switch(theValue) {
// Bool, Int, UInt, Float and Double are casted to NSNumber by default !?
// Bool, Int, UInt, Float and Double are casted to NSNumber by default!
case let numValue as NSNumber:
return (numValue, "NSNumber")
return (numValue, "NSNumber", false)
case let longValue as Int64:
return (NSNumber(longLong: longValue), "NSNumber")
return (NSNumber(longLong: longValue), "NSNumber", false)
case let longValue as UInt64:
return (NSNumber(unsignedLongLong: longValue), "NSNumber")
return (NSNumber(unsignedLongLong: longValue), "NSNumber", false)
case let intValue as Int32:
return (NSNumber(int: intValue), "NSNumber")
return (NSNumber(int: intValue), "NSNumber", false)
case let intValue as UInt32:
return (NSNumber(unsignedInt: intValue), "NSNumber")
return (NSNumber(unsignedInt: intValue), "NSNumber", false)
case let intValue as Int16:
return (NSNumber(short: intValue), "NSNumber")
return (NSNumber(short: intValue), "NSNumber", false)
case let intValue as UInt16:
return (NSNumber(unsignedShort: intValue), "NSNumber")
return (NSNumber(unsignedShort: intValue), "NSNumber", false)
case let intValue as Int8:
return (NSNumber(char: intValue), "NSNumber")
return (NSNumber(char: intValue), "NSNumber", false)
case let intValue as UInt8:
return (NSNumber(unsignedChar: intValue), "NSNumber")
return (NSNumber(unsignedChar: intValue), "NSNumber", false)
case let stringValue as String:
return (stringValue as NSString, "NSString")
return (stringValue as NSString, "NSString", false)
case let dateValue as NSDate:
return (dateValue, "NSDate", false)
case let anyvalue as NSArray:
return (anyvalue, valueType, false)
case let anyvalue as NSObject:
return (anyvalue, valueType)
return (anyvalue, valueType, true)
default:
assertionFailure("ERROR: valueForAny unkown type \(theValue), type \(valueType). Could not happen unless there will be a new type in Swift.")
return (NSNull(), "NSNull")
return (NSNull(), "NSNull", false)
}
}

Expand All @@ -525,7 +530,7 @@ final public class EVReflection {
// }
} else {
// Let us put a number into a string property by taking it's stringValue
let (_, type) = valueForAny("", key: key, anyValue: value)
let (_, type, _) = valueForAny("", key: key, anyValue: value)
if (typeInObject == "String" || typeInObject == "NSString") && type == "NSNumber" {
if let convertedValue = value as? NSNumber {
value = convertedValue.stringValue
Expand Down Expand Up @@ -772,18 +777,24 @@ final public class EVReflection {
if let (_, _, propertyGetter) = (theObject as? EVObject)?.propertyConverters().filter({$0.0 == key}).first {
value = propertyGetter()
}
var (unboxedValue, valueType): (AnyObject, String) = valueForAny(theObject, key: key, anyValue: value)
if unboxedValue as? EVObject != nil {
var (unboxedValue, valueType, isObject) = valueForAny(theObject, key: key, anyValue: value)
if isObject {
let (dict, _) = toDictionary(unboxedValue as! NSObject, performKeyCleanup: performKeyCleanup)
propertiesDictionary.setValue(dict, forKey: key)
} else if let array = unboxedValue as? [EVObject] {
var tempValue = [NSDictionary]()
for av in array {
let (dict, _) = toDictionary(av, performKeyCleanup: performKeyCleanup)
tempValue.append(dict)
} else if let array = unboxedValue as? [NSObject] {
let item = array.getArrayTypeInstance(array)
let (_,_,isObject) = valueForAny(anyValue: item)
if isObject {
var tempValue = [NSDictionary]()
for av in array {
let (dict, _) = toDictionary(av, performKeyCleanup: performKeyCleanup)
tempValue.append(dict)
}
unboxedValue = tempValue
propertiesDictionary.setValue(unboxedValue, forKey: key)
} else {
propertiesDictionary.setValue(unboxedValue, forKey: key)
}
unboxedValue = tempValue
propertiesDictionary.setValue(unboxedValue, forKey: key)
} else {
propertiesDictionary.setValue(unboxedValue, forKey: key)
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-->
[![Build Status](https://travis-ci.org/evermeer/EVReflection.svg?style=flat)](https://travis-ci.org/evermeer/EVReflection)
[![Issues](https://img.shields.io/github/issues-raw/evermeer/EVReflection.svg?style=flat)](https://github.com/evermeer/EVReflection/issues)
[![Coverage](https://img.shields.io/badge/coverage-99%-brightgreen.svg?style=flat)](https://raw.githubusercontent.com/evermeer/EVReflection/master/EVReflection/coverage.png)
[![Coverage](https://img.shields.io/badge/coverage-98%-brightgreen.svg?style=flat)](https://raw.githubusercontent.com/evermeer/EVReflection/master/EVReflection/coverage.png)
[![Documentation](https://img.shields.io/badge/documented-100%-brightgreen.svg?style=flat)](http://cocoadocs.org/docsets/EVReflection)
[![Stars](https://img.shields.io/github/stars/evermeer/EVReflection.svg?style=flat)](https://github.com/evermeer/EVReflection/stargazers)

Expand Down

0 comments on commit 6dcecba

Please sign in to comment.