How do you find out the type of an object in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Swift gives you several ways to inspect or narrow types at runtime, but each tool answers a slightly different question. Use is to test whether a value matches a type, as? to cast safely, and type(of:) when you want the runtime type itself for debugging or branching logic.
Check a Type with is
The is operator returns a boolean that tells you whether a value conforms to or inherits from a given type.
This is the right choice when you only need to know whether a value matches a type and do not need the typed value yet.
Safely Cast with as?
In real Swift code, type checking usually leads straight into conditional casting. as? returns an optional and avoids a crash when the cast fails.
This is often better than a separate is check because it both tests the type and gives you the typed result.
Use as! Only When Failure Is Impossible
Force casting exists, but it is a runtime trap if your assumption is wrong.
Use this only when the surrounding program logic guarantees the type. In most application code, as? is safer and easier to maintain.
Inspect the Runtime Type with type(of:)
If you need the concrete type for logging or debugging, type(of:) returns it directly.
This is useful when values are stored as Any, protocol types, or superclass references and you want to see the actual runtime type.
Combine Type Checks in switch
For heterogeneous collections, switch often produces the clearest code.
This reads better than a chain of repeated if statements once the number of candidate types grows.
Check Protocol Conformance Too
Sometimes the question is not the concrete class, but whether the value supports a capability. Protocol casting works the same way as type casting.
That is usually more flexible than checking for one exact concrete type.
Prefer Strong Types Over Runtime Inspection
Frequent runtime type checks can signal that the model is too weakly typed. If many values live as Any, you may be fighting the language instead of using it. Swift works best when types are explicit and the compiler can help you, so treat runtime inspection as a targeted tool, not a default design style.
Common Pitfalls
- Using
as!where a failed cast is possible and turning bad input into a crash. - Calling
type(of:)when the real need is a safe cast to a usable type. - Overusing
Anyand then compensating with repeated runtime checks. - Checking concrete classes when protocol conformance would be the better abstraction.
- Splitting type checks and casts into separate steps when
as?can do both cleanly.
Summary
- Use
isfor a simple type test. - Use
as?for safe casting and typed access. - Use
as!only when the cast cannot fail. - Use
type(of:)for runtime inspection and debugging. - Prefer stronger compile-time types so runtime checks stay minimal.
Related reading
- How do you get an iPhone's device name
- How do you hide the warnings in React Native iOS simulator?
- How do you install an APK file in the Android emulator?
- How do you install an APK file in the Android emulator?
- How do you install Google frameworks Play, Accounts, etc. on a Genymotion virtual device?
- How do you load custom UITableViewCells from Xib files?
- How do you loop AVPlayer in Swift?
- How do you make a LinearLayout scrollable?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.