How to Use Type Casting in Swift
Use type casting in Swift when a value stored as a broader type needs to be checked or converted into a more specific subtype. This is common with class inheritance, protocol collections, UIKit hierarchies, and mixed model arrays.
What you’ll build or solve
You’ll learn how to use is, as?, and as! in Swift for safe type checks, optional downcasting, and forced casts. You’ll also know when polymorphism removes the need to cast at all.
Learn Swift on Mimo
When this approach works best
This approach is the right choice when runtime type differences matter.
Common real-world scenarios include:
UIViewsubclasses- Table cell casting
- Mixed model arrays
- Protocol-based collections
- Media item hierarchies
This is a bad idea when the design can be improved with protocols or shared base methods instead of repeated casting.
Prerequisites
You only need:
- Basic Swift classes
- Inheritance
- Familiarity with optionals
Step-by-step instructions
Step 1: Check a type with is
Use is to test the runtime type.
Swift
if view is UIButton {
print("This is a button")
}
This only checks the type without converting it.
It is useful for branching logic.
Step 2: Safely downcast with as?
The safest cast is optional casting.
Swift
if let button = view as? UIButton {
button.setTitle("Save", for: .normal)
}
This only runs when the cast succeeds.
It is the default production-safe choice.
Step 3: Force cast with as!
Use forced casts only when the type is guaranteed.
Swift
let button = view as! UIButton
This crashes if the assumption is wrong.
It is mostly safe in tightly controlled contexts.
Step 4: Cast mixed arrays
A common real-world pattern is heterogeneous collections.
Swift
let items: [Any] = ["Swift", 42, true]
for item in items {
if let text = item as? String {
print(text.uppercased())
}
}
This safely handles mixed values.
What to look for:
ischecks type onlyas?safely casts to optionalsas!force casts and can crash- Great for UI hierarchies
- Prefer safer architecture when possible
Examples you can copy
Safe button cast
Swift
if let button = view as? UIButton
Force cast
Swift
let cell = tableViewCell as! CustomCell
Mixed array
Swift
if let number = item as? Int
Common mistakes and how to fix them
Mistake 1: Overusing as!
What the reader might do:
Force-cast uncertain network models.
Why it breaks: unexpected runtime crashes happen.
Corrected approach:
Use as?.
Mistake 2: Repeated casting in bad architecture
What the reader might do:
Cast the same protocol type in many places.
Why it breaks: the design likely needs better polymorphism.
Corrected approach:
Move shared behavior into protocols or base classes.
Mistake 3: Forgetting optional unwrap after as?
What the reader might do:
Swift
let button = view as? UIButton
button.setTitle(...)
Why it breaks: the result is optional.
Corrected approach:
Use if let.
Troubleshooting
If crashes happen, replace as!.
If repeated casts appear, improve the type design.
If optional casts fail, inspect the real runtime type.
If UI hierarchies feel noisy, use custom subclasses earlier.
Quick recap
- Use
isfor type checks - Use
as?for safe optional casts - Use
as!only when guaranteed safe - Great for UI and mixed arrays
- Prefer polymorphism over repeated casting
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot