Is there any easy way to tell what class
an instance variable is in a Swift? In the JVM-based languages that I'm used to, you can do something like println(value.class)
to get it's class.
Is there something equivalent in Swift?
The closest thing I can find in the docs is the ability to do "type checking" with the is <Class>
keyword but that only helps me guess a little bit.
I've run into a few situations in playing around where I thought I had one type of class, but actually had another and didn't know how to know for sure.
An instance variable is not a class variable although there are similarities. It is a type of class attribute (or class property, field, or data member). The same dichotomy between instance and class members applies to methods ("member functions") as well; a class may have both instance methods and class methods.
“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.
Defining Classes in Swift To declare a class in Swift, you use the class keyword followed by the name of the class. If it has a superclass, you add a colon and the name of the superclass. The beginning and end of the class are indicated by the opening and closing curly braces.
Instance Variable: It is basically a class variable without a static modifier and is usually shared by all class instances. Across different objects, these variables can have different values.
Use type.self
to return a type that can be passed into a method that accepts a type-level argument. For example, UILabel.self
can be passed to the isKindOfClass
method call. The string representation of the class can be found via dynamicType.description()
:
var label = UILabel()
println(label.dynamicType.description())
println(label.isKindOfClass(UILabel.self))
Swift-3
var label = UILabel()
println(type(of: label).description())
Output
UILabel
true
Here's a bit more background -- there are two expressions to be aware of: the postfix self expression and the dynamic type expression. From the docs:
Postfix Self
A postfix self expression consists of an expression or the name of a type, immediately followed by .self. It has the following forms:expression.self type.self
The first form evaluates to the value of the expression. For example, x.self evaluates to x.
The second form evaluates to the value of the type. Use this form to access a type as a value. For example, because SomeClass.self evaluates to the SomeClass type itself, you can pass it to a function or method that accepts a type-level argument
Dyamic Type Expression
A dynamicType expression consists of an expression, immediately followed by .dynamicType. It has the following form:expression.dynamicType
The expression can’t be the name of a type. The entire dynamicType expression evaluates to the value of the runtime type of the expression.
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