Skip to content

Commit

Permalink
working with arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Nov 27, 2015
1 parent 5e85743 commit f9551db
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 54 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 = "2.12.2"
s.version = "2.12.3"
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
37 changes: 0 additions & 37 deletions EVReflection/EVReflectionTests/EVObjectDescriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,47 +75,10 @@ class EVObjectDescriptionTests: XCTestCase {
}
}

func testX() {
let x = BoundRequest()
print(x.toJsonString())
}

}



class BoundRequest:EVObject{
var action:String = "byBounds"
var platform:String = "android"
var types:[String]? = ["a","b"]
var bounds:Bound = Bound()
}

//Why does Mirror retuns an extra child with the label _core when I add: var types:[String]? = ["a","b"]

class Bound:EVObject {
var ne:Coordinate = Coordinate()
var sw:Coordinate = Coordinate()
}

class Coordinate:EVObject{
var lat:Double=0
var lng:Double=0

// func getCoordinate()->CLLocationCoordinate2D{
// return CLLocationCoordinate2D(latitude: lat, longitude: lng)
// }
//
// func setLatLng(lat:Double!,lng:Double!){
// self.lat=lat
// self.lng=lng
// }
//
// func setLatLng(latLng:CLLocationCoordinate2D){
// self.lat = latLng.latitude
// self.lng = latLng.longitude
// }
}



Expand Down
8 changes: 8 additions & 0 deletions EVReflection/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ class EVReflectionTests: XCTestCase {
let result = TestObject3(dictionary: toDict)
XCTAssert(theObject != result, "Pass") // The objects are not the same
}


/**
Test if we can work with an object that contains all types of arrays
*/
func testArrays() {
let x = ArrayObjects()
print(x.toJsonString())
}
}


Expand Down
16 changes: 16 additions & 0 deletions EVReflection/EVReflectionTests/TestObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,20 @@ public class TestObject6: EVObject {
}


/**
For testing objects with arrays
*/

class ArrayObjects:NSObject {
var strings:[String] = ["a","b"]
var dates:[NSDate] = [NSDate(), NSDate()]
var arrays:[[String]] = [["a","b"],["c","d"]]
var dictionaries:[NSDictionary] = [NSDictionary(), NSDictionary()]
var subobjects:[SubObject] = [SubObject(), SubObject()]
}

class SubObject:NSObject {
var field:String = "x"
}


33 changes: 17 additions & 16 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -773,16 +773,18 @@ final public class EVReflection {
}
for property in reflected.children {
if let key:String = property.label {
if key != "_core" {
var value = property.value
if let (_, _, propertyGetter) = (theObject as? EVObject)?.propertyConverters().filter({$0.0 == key}).first {
value = propertyGetter()
}
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? [NSObject] {
var value = property.value
if let (_, _, propertyGetter) = (theObject as? EVObject)?.propertyConverters().filter({$0.0 == key}).first {
value = propertyGetter()
}
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? [NSObject] {
if unboxedValue as? [String] != nil || unboxedValue as? [NSString] != nil || unboxedValue as? [NSDate] != nil || unboxedValue as? [NSNumber] != nil || unboxedValue as? [NSArray] != nil || unboxedValue as? [NSDictionary] != nil {
propertiesDictionary.setValue(unboxedValue, forKey: key)
} else {
let item = array.getArrayTypeInstance(array)
let (_,_,isObject) = valueForAny(anyValue: item)
if isObject {
Expand All @@ -794,16 +796,15 @@ final public class EVReflection {
unboxedValue = tempValue
propertiesDictionary.setValue(unboxedValue, forKey: key)
} else {
propertiesDictionary.setValue(unboxedValue, forKey: key)
propertiesDictionary.setValue(unboxedValue, forKey: key)
}
} else {
propertiesDictionary.setValue(unboxedValue, forKey: key)
}
propertiesTypeDictionary[key] = valueType
} else {
propertiesDictionary.setValue(unboxedValue, forKey: key)
}

propertiesTypeDictionary[key] = valueType
}

}
return (propertiesDictionary, propertiesTypeDictionary)
}
Expand Down

0 comments on commit f9551db

Please sign in to comment.