ARC basics: strong / weak / unowned
Learn ARC reference counting and when to use strong , weak , and unowned to avoid memory leaks.
ARC basics: strong / weak / unowned is a free Swift Academy lesson on CoddyKit — lesson 1 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
ARC in one minute
Swift uses ARC (Automatic Reference Counting). Each strong reference increases a class instance's count. When it drops to zero, deinit runs and memory is freed.
Strong by default
Strong is the default. When the last strong reference goes away, deinit is called.
final class Person {
let name: String
init(_ name: String) { self.name = name; print("init:", name) }
deinit { print("deinit:", name) }
}
var p: Person? = Person("Ada")
p = nil // ARC count -> 0, triggers deinitCycle example
Two-way strong references create a retain cycle. ARC cannot free them, causing a leak.
final class A {
var b: B?
deinit { print("A deinit") }
}
final class B {
var a: A?
deinit { print("B deinit") }
}
var a: A? = A()
var b: B? = B()
a?.b = b // strong
b?.a = a // strong
a = nil // no deinit (cycle)
b = nil // still no deinit (leak)weak reference
Make one side weak to avoid the cycle. Weak refs are optional and become nil automatically when the object deallocates.
final class A2 {
weak var b: B2? // weak breaks the strong cycle
deinit { print("A2 deinit") }
}
final class B2 {
var a: A2?
deinit { print("B2 deinit") }
}
var a2: A2? = A2()
var b2: B2? = B2()
a2?.b = b2 // weak
b2?.a = a2 // strong
a2 = nil // A2 deinit runs
b2 = nil // B2 deinit runsunowned reference
Use unowned when the referenced object will outlive this reference. It is non-optional and not set to nil; accessing it after dealloc is a crash—so guarantee lifetimes.
final class Customer {
let name: String
var card: CreditCard?
init(_ name: String) { self.name = name }
deinit { print("Customer deinit") }
}
final class CreditCard {
unowned let owner: Customer // never optional; owner must outlive card
init(owner: Customer) { self.owner = owner }
deinit { print("CreditCard deinit") }
}
var cust: Customer? = Customer("Lars")
cust!.card = CreditCard(owner: cust!)
cust = nil // deallocates both (no cycle)Tips & patterns
Tips:
- Default to strong.
- Break cycles with weak (delegates are commonly weak).
- Use unowned only when lifetime is guaranteed (e.g., child → parent).
- Classes are reference types; structs/enums don’t need ARC rules.
weak vs unowned?
Quick check: When do you choose weak vs unowned?
Recap
Recap: ARC frees objects when no strong refs remain. Prevent cycles with weak (optional) or unowned (lifetime guaranteed). Choose the smallest safe ownership.
Frequently asked questions
Is the “ARC basics: strong / weak / unowned” lesson free?
Yes — the full text of “ARC basics: strong / weak / unowned” is free to read here on the web, and the Swift Academy course includes 3 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 “ARC basics: strong / weak / unowned”?
Learn ARC reference counting and when to use strong , weak , and unowned to avoid memory leaks. 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 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “ARC basics: strong / weak / unowned” 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
- ARC basics: strong / weak / unowned
- Retain cycles & breaking with weak/unowned
- Value types to reduce sharing