I want to know what the type is of a AnyObject variable that i initialise later. For example:
var test: AnyObject
test = 12.2
I cant figure out how to do this.
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.
Swift – Check if Variable/Object is String To check if a variable or object is a String, use is operator as shown in the following expression. where x is a variable/object. The above expression returns a boolean value: true if the variable is a String, or false if not a String.
“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, you can print a variable or a constant to the screen using the print() function.
You can do this with the is
operator.
Example code:
var test: AnyObject
test = 12.2
if test is Double {
println("Double type")
} else if test is Int {
println("Int type")
} else if test is Float {
println("Float type")
} else {
println("Unkown type")
}
According to Apple docs:
Checking Type
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.
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