Swift Academy · Lesson

Type Casting: is, as?, as! and as

Checking and converting types safely with Swift's casting operators.

Lesson 3 of 413 steps

Type Casting: is, as?, as! and as is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

What is Type Casting?

Type casting lets you check or convert an instance to a different class in its hierarchy.

class Animal { var name: String = "Animal" }
class Dog: Animal { func bark() { print("Woof") } }
let a: Animal = Dog()
print(a.name)

The is Operator

Use is to check whether an instance is of a certain subclass.

let a: Animal = Dog()
if a is Dog {
  print("It's a dog")
}

Downcasting with as?

as? attempts a downcast and returns an Optional. If it fails, the result is nil.

let a: Animal = Dog()
if let dog = a as? Dog {
  dog.bark()
} else {
  print("Not a dog")
}

Downcasting with as!

as! force-casts to a subtype. Use only when you are certain of the type; crashes if wrong.

let a: Animal = Dog()
let dog = a as! Dog
dog.bark()

Upcasting with as

Upcasting from a subclass to a superclass always succeeds and uses as.

let dog = Dog()
let animal = dog as Animal
print(animal.name)

Type Casting in Collections

Mixed-type arrays typed as [Any] require casting to use specific behaviour.

let items: [Any] = [1, "hello", Dog()]
for item in items {
  if let d = item as? Dog { d.bark() }
  if let s = item as? String { print(s) }
}

Type Casting with AnyObject

AnyObject represents any class instance. Use as? to extract the concrete type.

let objs: [AnyObject] = [Dog(), Dog()]
for obj in objs {
  if let d = obj as? Dog { d.bark() }
}

Casting Protocol Types

You can cast between protocol types and concrete types using the same operators.

protocol Runnable { func run() }
class Cat: Animal, Runnable { func run() { print("Cat runs") } }
let r: Runnable = Cat()
if let cat = r as? Cat { print(cat.name) }

Pattern Matching with is in switch

Swift's switch statement can match on type using the is pattern.

let value: Any = 42
switch value {
case is Int: print("Integer")
case is String: print("String")
default: print("Other")
}

as? in switch Patterns

Combine switch with let x as Type to both check and bind in one step.

let value: Any = "Swift"
switch value {
case let s as String: print("String: \(s)")
case let i as Int: print("Int: \(i)")
default: break
}

Safe Casting Best Practices

Always prefer as? over as! to avoid runtime crashes in production code.

func describe(_ obj: AnyObject) {
  if let dog = obj as? Dog { dog.bark(); return }
  print("Unknown type")
}

Quick Check

Which operator returns an Optional when downcasting?

Lesson Recap

Swift type casting: is checks type, as? safely downcasts to Optional, as! force-downcasts, as upcasts. Prefer as? in production code.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Type Casting: is, as?, as! and as” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Type Casting: is, as?, as! and as”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Type Casting: is, as?, as! and as”?

Checking and converting types safely with Swift's casting operators. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Type Casting: is, as?, as! and as” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Class Inheritance and the super Keyword
  2. override, final and Preventing Subclassing
  3. Type Casting: is, as?, as! and as
  4. Deinitializers and Memory Lifecycle
← Back to Swift Academy