Is there a function to determine the variable type in Swift? I presume there might be something like like type() in Python.
I'd like a way to judge if a variable is a Foundation object or C variable in Swift. Like NSString vs String, or NSArray vs array. So that I can log it out in console and see clearly what it is.
For example, I would like to know the type inferred for the the first array below:
var array = [1,2,3] // by default NSArray or array? var array:[Int] = [1,2,3] var array:NSArray = [1,2,3] var array:Array<Any> = [1,2,3] I have seen answers for judging if a given variable is a kind of given type in this question, but I'll say it's quite different from what I want to ask.
To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.
In Swift, there are two kinds of types: named types and compound types. A named type is a type that can be given a particular name when it's defined. Named types include classes, structures, enumerations, and protocols.
“Use the type check operator (is) to check whether an instance is of a certain subclass type. The type check operator returns true if the instance is of that subclass type and false if it is not.” Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.
In swift, we use the var keyword to declare a variable. Swift uses variables to store and refer to values by identifying their name. Variables must be declared before they are used.
You can get a reference to the type object of a value by using the .dynamicType property. This is equivalent to Python's type() function, and is mentioned in the Swift documentation under Language Reference: Types: Metatype Type.
var intArray = [1, 2, 3] let typeOfArray = intArray.dynamicType With this type object, we are able to create a new instance of the same array type.
var newArray = typeOfArray() newArray.append(5) newArray.append(6) println(newArray) [5, 6] We can see that this new value is of the same type ([Int]) by attempting to append a float:
newArray.append(1.5) error: type 'Int' does not conform to protocol 'FloatLiteralConvertible' If we import Cocoa and use an array literal with mixed types, we can see that an NSArray is created:
import Cocoa var mixedArray = [1, "2"] let mixedArrayType = mixedArray.dynamicType var newArray = mixedArrayType() var mutableArray = newArray.mutableCopy() as NSMutableArray mutableArray.addObject(1) mutableArray.addObject(1.5) mutableArray.addObject("2") println(mutableArray) (1, "1.5", 2) However, at this point there does not seem to be any general way to generate a string description of a type object, so this may not serve the debugging role that you were asking about.
Types derived from NSObject do have a .description() method, as is used in SiLo's answer,
println(mixedArrayType.description()) __NSArrayI However this is not present on types such as Swift's built-in arrays.
println(typeOfArray.description()) error: '[Int].Type' does not have a member named 'description'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With