Skip to content

Instantly share code, notes, and snippets.

@0xWDG
Last active March 19, 2016 21:35
Show Gist options
  • Save 0xWDG/5cf0f2187c7d31a931b7 to your computer and use it in GitHub Desktop.
Save 0xWDG/5cf0f2187c7d31a931b7 to your computer and use it in GitHub Desktop.
get the classname back from swift.

Sometimes swift will get some messy by returning _NSContiguousString instead of String if you use .dynamicType

this function will try to help you, to get the class with the easiest way, taking no futher efford.

//
// SwiftGetHumanType.swift
// PHPFramework
//
// Created by Wesley de Groot on 02-03-16.
// Copyright © 2016 WDGWV. All rights reserved.
//
// https://gist.github.com/wdg/5cf0f2187c7d31a931b7
import Foundation
func getHumanClassname(Ob: Any) -> String {
var _ret: String
switch (String(Ob.dynamicType)) {
case "__NSCFNumber", "Int":
_ret = "Int"
break
case "__NSCFBoolean", "Bool":
_ret = "Bool"
break
case "Double":
_ret = "Double"
break
case "_SwiftDeferredNSArray", "Array<String>", "Array<Int>":
_ret = "Array"
break
case "__NSCFString", "_NSContiguousString", "String", "NSString":
_ret = "String"
break
case "_NativeDictionaryStorageOwner<String, Array<String>>":
_ret = "Dictionary"
break
case "protocol<>", "protocol<> -> Bool":
_ret = "Protocol"
break
default:
print("Did'nt found type!, please report it to: https://gist.github.com/wdg/5cf0f2187c7d31a931b7")
print("Found: \(String(Ob.dynamicType))")
_ret = "\(String(Ob.dynamicType))"
break
}
return _ret
}
func getClass(Ob: Any) -> String {
return getHumanClassname(Ob)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment