How do I print the type or class of a variable in Swift?

Answer

You can use the _stdlib_getDemangledTypeName(). Paste this into an empty playground:

import Foundation

class PureSwiftClass {
}


var myvar0 = NSString() // Objective-C class
var myvar1 = PureSwiftClass()
var myvar2 = 42
var myvar3 = "Hans"

println( "TypeName0 = \(_stdlib_getDemangledTypeName(myvar0))")
println( "TypeName1 = \(_stdlib_getDemangledTypeName(myvar1))")
println( "TypeName2 = \(_stdlib_getDemangledTypeName(myvar2))")
println( "TypeName3 = \(_stdlib_getDemangledTypeName(myvar3))")
and get this as output:

TypeName0 = NSString
TypeName1 = __lldb_expr_26.PureSwiftClass
TypeName2 = Swift.Int
TypeName3 = Swift.String
Original answer:

Prior to Xcode 6.3 _stdlib_getTypeName got the mangled type name of a variable. Ewan Swick's blog entry helps to decipher these strings:

e.g. _TtSi stands for Swift's internal Int type.

All swift Questions

Ask your interview questions on swift

Write Your comment or Questions if you want the answers on swift from swift Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---