Skip to content

Commit

Permalink
workaround fix for nested generics
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Aug 10, 2016
1 parent 4926c0d commit 6654b98
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 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.44.0"
s.version = "2.45.0"
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public class MyGenericObject<T where T:NSObject>: MyGenericBase, EVGenericsKVC {
print("---> setValue '\(value)' for key '\(key)' should be handled.")
}
}

public func getGenericType() -> NSObject {
return T()
}
}


Expand All @@ -122,6 +126,10 @@ public class MyIncorrectGenericObject<T where T:NSObject>: MyGenericBase, EVGene
required public init() {
super.init()
}

public func getGenericType() -> NSObject {
return T()
}
}


Expand Down
13 changes: 8 additions & 5 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -961,18 +961,18 @@ final public class EVReflection {
let t: String = (onlyElement?.key as? String) ?? ""
if onlyElement?.value as? NSArray != nil && type.lowercaseString == "array<\(t)>" {
dictValue = onlyElement?.value as? NSArray
dictValue = dictArrayToObjectArray(type, array: (dictValue as? [NSDictionary]) ?? [NSDictionary](), conversionOptions: conversionOptions) as NSArray
dictValue = dictArrayToObjectArray(anyObject, type: type, array: (dictValue as? [NSDictionary]) ?? [NSDictionary](), conversionOptions: conversionOptions) as NSArray
} else {
// Single object array fix
var array: [NSDictionary] = [NSDictionary]()
array.append(dictValue as? NSDictionary ?? NSDictionary())
dictValue = dictArrayToObjectArray(type, array: array, conversionOptions: conversionOptions) as NSArray
dictValue = dictArrayToObjectArray(anyObject, type: type, array: array, conversionOptions: conversionOptions) as NSArray
}
} else {
// Single object array fix
var array: [NSDictionary] = [NSDictionary]()
array.append(dictValue as? NSDictionary ?? NSDictionary())
dictValue = dictArrayToObjectArray(type, array: array, conversionOptions: conversionOptions) as NSArray
dictValue = dictArrayToObjectArray(anyObject, type: type, array: array, conversionOptions: conversionOptions) as NSArray
}
} else if let _ = type.rangeOfString("_NativeDictionaryStorageOwner"), let dict = dictValue as? NSDictionary, let org = anyObject as? EVObject {
dictValue = org.convertDictionary(key, dict: dict)
Expand All @@ -982,7 +982,7 @@ final public class EVReflection {
valid = isValid
} else if type.rangeOfString("<NSDictionary>") == nil && dictValue as? [NSDictionary] != nil {
// Array of objects
dictValue = dictArrayToObjectArray(type, array: dictValue as? [NSDictionary] ?? [NSDictionary](), conversionOptions: conversionOptions) as NSArray
dictValue = dictArrayToObjectArray(anyObject, type: type, array: dictValue as? [NSDictionary] ?? [NSDictionary](), conversionOptions: conversionOptions) as NSArray
} else if original is EVObject && dictValue is String {
// fixing the conversion from XML without properties
let (dict, isValid) = dictToObject(type, original:original, dict: ["__text": dictValue as? String ?? ""], conversionOptions: conversionOptions)
Expand Down Expand Up @@ -1042,7 +1042,7 @@ final public class EVReflection {

- returns: The array of objects that is created from the array of dictionaries
*/
private class func dictArrayToObjectArray(type: String, array: NSArray, conversionOptions: ConversionOptions = .DefaultDeserialize) -> NSArray {
private class func dictArrayToObjectArray(anyObject: NSObject, type: String, array: NSArray, conversionOptions: ConversionOptions = .DefaultDeserialize) -> NSArray {
var subtype = "EVObject"
if type.componentsSeparatedByString("<").count > 1 {
// Remove the Array prefix
Expand All @@ -1062,6 +1062,9 @@ final public class EVReflection {
if let evResult = org as? EVObject {
org = evResult.getSpecificType(item as? NSDictionary ?? NSDictionary())
}
if let evResult = anyObject as? EVGenericsKVC {
org = evResult.getGenericType()
}
let (arrayObject, valid) = dictToObject(subtype, original:org, dict: item as? NSDictionary ?? NSDictionary(), conversionOptions: conversionOptions)
if arrayObject != nil && valid {
result.append(arrayObject!)
Expand Down
5 changes: 5 additions & 0 deletions EVReflection/pod/EVWorkaroundHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public protocol EVGenericsKVC {
Implement this protocol in a class with generic properties so that we can still use a standard mechanism for setting property values.
*/
func setValue(value: AnyObject!, forUndefinedKey key: String)

/**
Add a function so that we can get an instance of T
*/
func getGenericType() -> NSObject
}

/**
Expand Down

0 comments on commit 6654b98

Please sign in to comment.