SwiftMidMCQ

What is the difference between `strong`, `weak`, and `unowned` references?

Test your knowledge:

Explanation & Code

Answer: These determine how ARC (Automatic Reference Counting) manages object lifetimes.

  • strong — increments the retain count. Default for most references.
  • weak — does NOT increment retain count. Automatically set to nil when the object deallocates. Must be Optional.
  • unowned — does NOT increment retain count. Assumed to always have a value — crashes if accessed after deallocation.

Code Example:

class Owner {
    var pet: Pet?
}

class Pet {
    weak var owner: Owner?  // avoid retain cycle
}

// unowned — use when you're certain the referenced object outlives the current one
class ViewModel {
    unowned let coordinator: AppCoordinator
    init(coordinator: AppCoordinator) {
        self.coordinator = coordinator
    }
}

When to use weak vs unowned:

  • weak — the reference can become nil (delegate patterns, optional parent refs)
  • unowned — the reference should never become nil (e.g. child → parent with guaranteed lifetime)

Rate your understanding:

Ready to practice more Swift?

Test yourself with our interactive quiz mode or browse all curated questions for this topic.