Get a user-readable version of the class name in swift (in objc NSStringFromClass was fine)
You can now just do:
String(MyClass)
new format based on xcode 6 beta 5.
(project_name).(class_name)
func getName(classType:AnyClass) -> String {
let classString = NSStringFromClass(classType.self)
let range = classString.rangeOfString(".", options: NSStringCompareOptions.CaseInsensitiveSearch, range: Range<String.Index>(start:classString.startIndex, end: classString.endIndex), locale: nil)
return classString.substringFromIndex(range!.endIndex)
}
Latest 6.3 Xcode Swift 1.2
if you need an extension or you can put this on any common object:
public extension NSObject{
public class var nameOfClass: String{
return NSStringFromClass(self).componentsSeparatedByString(".").last!
}
public var nameOfClass: String{
return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
}
}
Swift 3
type(of: self)
prints ModuleName.ClassName
String(describing: type(of: self))
prints ClassName