Skip to content

Commit

Permalink
swiftClassTypeFromString fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Mar 31, 2016
1 parent a18dd7c commit 1991d11
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 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.22.1"
s.version = "2.22.2"
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
13 changes: 12 additions & 1 deletion EVReflection/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,17 @@ class EVReflectionTests: XCTestCase {
print(a2.toJsonString())
}

func testNestedArry() {
let json = "{\"date\": 2457389.3333330001,\"results\": { \"sun\": [[559285.95145709824, 202871.33591198301, 61656.198554897906], [127.6163120820332, 948.44727756795123, 406.68471093096883]], \"geomoon\": [[-401458.60657087743, -43744.769596474769, -11058.709613333322], [8433.3114508170656, -78837.790870237863, -26279.67592282737]] }, \"unit\": \"km\" }"
let myObject = NestedArrays(json: json)
print(myObject.toJsonString())
}

func testStruct() {
//TODO: Fix structs
let json = X().toJsonString()
print("json = \(json)")
}
}
}

class X: EVObject {
Expand All @@ -316,3 +323,7 @@ class X: EVObject {







22 changes: 22 additions & 0 deletions EVReflection/EVReflectionTests/TestObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,25 @@ public class AA : EVObject{
public class BB : EVObject{
public var val : Int = 0
}

class NestedArrays: EVObject {
var date: NSNumber?
var results: NestedArraysResult?
var unit: String?
}

class NestedArraysResult: EVObject {
var planets = [String: [[NSNumber]]]()

// This way we can solve that the JSON has arbitrary keys
internal override func setValue(value: AnyObject!, forUndefinedKey key: String) {
if let a = value as? [[NSNumber]] {
planets[key] = a
return
}
NSLog("---> setValue for key '\(key)' should be handled.")
}
}



25 changes: 10 additions & 15 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -446,33 +446,28 @@ final public class EVReflection {

:returns: The Class type
*/
public class func swiftClassTypeFromString(className: String) -> AnyClass! {
// if className.hasPrefix("Optional<") {
// className = className.substringWithRange(Range<String.Index>(start: className.startIndex.advancedBy(9), end: className.endIndex.advancedBy(-1)))
// }
if className.hasPrefix("_Tt") {
return NSClassFromString(className)
public class func swiftClassTypeFromString(className: String) -> AnyClass? {
if let c = NSClassFromString(className) {
return c
}
var classStringName = className

// The default did not work. try a combi of appname and classname
if className.rangeOfString(".", options: NSStringCompareOptions.CaseInsensitiveSearch) == nil {
let appName = getCleanAppName()
classStringName = "\(appName).\(className)"
}

if let classStringName = NSClassFromString(classStringName) {
return classStringName
if let c = NSClassFromString("\(appName).\(className)") {
return c
}
}

if let bundleIdentifiers = bundleIdentifiers {
for aBundle in bundleIdentifiers {
let className = "\(aBundle).\(className)"
if let existingClass = NSClassFromString(className) {
if let existingClass = NSClassFromString("\(aBundle).\(className)") {
return existingClass
}
}
}

return NSClassFromString(classStringName)
return nil
}

/**
Expand Down

0 comments on commit 1991d11

Please sign in to comment.