Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find out the type of an object (in Swift)?

When trying to understand a program, or in some corner-cases, it's useful to find out what type something is. I know the debugger can show you some type information, and you can usually rely on type inference to get away with not specifying the type in those situations, but still, I'd really like to have something like Python's type()

dynamicType (see this question)

Update: this has been changed in a recent version of Swift, obj.dynamicType now gives you a reference to the type and not the instance of the dynamic type.

This one seems the most promising, but I haven't been able to find out the actual type so far.

class MyClass {     var count = 0 }  let mc = MyClass()  # update: this now evaluates as true mc.dynamicType === MyClass.self 

I also tried using a class reference to instantiate a new object, which does work, but oddly gave me an error saying I must add a required initializer:

works:

class MyClass {     var count = 0     required init() {     } }  let myClass2 = MyClass.self let mc2 = MyClass2() 

Still only a small step toward actually discovering the type of any given object though

edit: I've removed a substantial number of now irrelevant details - look at the edit history if you're interested :)

like image 389
Jiaaro Avatar asked Jun 07 '14 20:06

Jiaaro


People also ask

How do you determine the type of object?

Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.

How can I check if an object is of a given type in 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. “The Swift Programming Language.” iBooks.

What is the type of a type in Swift?

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. For example, instances of a user-defined class named MyClass have the type MyClass .

What are the three object types in Swift?

As I mentioned in Chapter 1, Swift object types come in three flavors: enum, struct, and class.


2 Answers

Swift 3 version:

type(of: yourObject) 
like image 124
Jérémy Lapointe Avatar answered Dec 03 '22 10:12

Jérémy Lapointe


Swift 2.0:

The proper way to do this kind of type introspection would be with the Mirror struct,

    let stringObject:String = "testing"     let stringArrayObject:[String] = ["one", "two"]     let viewObject = UIView()     let anyObject:Any = "testing"      let stringMirror = Mirror(reflecting: stringObject)     let stringArrayMirror = Mirror(reflecting: stringArrayObject)     let viewMirror = Mirror(reflecting: viewObject)     let anyMirror = Mirror(reflecting: anyObject) 

Then to access the type itself from the Mirror struct you would use the property subjectType like so:

    // Prints "String"     print(stringMirror.subjectType)      // Prints "Array<String>"     print(stringArrayMirror.subjectType)      // Prints "UIView"     print(viewMirror.subjectType)      // Prints "String"     print(anyMirror.subjectType) 

You can then use something like this:

    if anyMirror.subjectType == String.self {         print("anyObject is a string!")     } else {         print("anyObject is not a string!")     } 
like image 44
Gudbergur Avatar answered Dec 03 '22 10:12

Gudbergur