Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell what the class of an instance variable is in Swift

Tags:

swift

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.

like image 740
Ted Naleid Avatar asked Jun 07 '14 23:06

Ted Naleid


People also ask

Can an instance variable be a class?

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.

How do you check if an object is of a certain class Swift?

“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.

How do you define a class variable in Swift?

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.

What is or are the instance variables in this class?

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.


1 Answers

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.

like image 175
memmons Avatar answered Oct 21 '22 13:10

memmons