0Pricing
Swift Academy · Lesson

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

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

Type Casting: is, as?, as! and as is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Type Casting: is, as?, as! and as” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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