Challenge - 5 Problems
Unowned Reference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of unowned reference usage in Swift
What is the output of this Swift code using an unowned reference?
Swift
class Owner { var name: String init(name: String) { self.name = name } deinit { print("Owner \(name) is being deinitialized") } } class Dependent { unowned let owner: Owner init(owner: Owner) { self.owner = owner } func printOwnerName() { print("Owner's name is \(owner.name)") } } var owner: Owner? = Owner(name: "Alice") var dependent: Dependent? = Dependent(owner: owner!) dependent?.printOwnerName() owner = nil print("End of program")
Attempts:
2 left
💡 Hint
Think about when the deinitializer runs and when the unowned reference is accessed.
✗ Incorrect
The unowned reference does not increase the reference count. The owner is deinitialized after setting owner = nil. The dependent prints the owner's name before that. So the output shows the print, then deinit message, then final print.
❓ Predict Output
intermediate2:00remaining
What happens if unowned reference is accessed after owner is nil?
What error occurs when accessing an unowned reference after its owner is deallocated?
Swift
class Owner { var name: String init(name: String) { self.name = name } deinit { print("Owner \(name) deinitialized") } } class Dependent { unowned let owner: Owner init(owner: Owner) { self.owner = owner } func printOwnerName() { print("Owner's name is \(owner.name)") } } var owner: Owner? = Owner(name: "Bob") var dependent: Dependent? = Dependent(owner: owner!) owner = nil // Now accessing dependent.owner dependent?.printOwnerName()
Attempts:
2 left
💡 Hint
Unowned references do not become nil automatically. What happens if you access them after deallocation?
✗ Incorrect
Accessing an unowned reference after its owner is deallocated causes a runtime crash with a message about unexpectedly found nil while unwrapping an Optional value.
🔧 Debug
advanced2:30remaining
Identify the cause of runtime crash with unowned reference
Why does this Swift code crash at runtime?
Swift
class Person { let name: String unowned var apartment: Apartment init(name: String, apartment: Apartment) { self.name = name self.apartment = apartment } } class Apartment { let unit: String var tenant: Person? init(unit: String) { self.unit = unit } } var apt: Apartment? = Apartment(unit: "4B") var person: Person? = Person(name: "John", apartment: apt!) apt?.tenant = person apt = nil person?.apartment.unit
Attempts:
2 left
💡 Hint
Consider the lifetime of apartment and how unowned references behave when the referenced object is deallocated.
✗ Incorrect
The apartment is set to nil and deallocated, but the person still has an unowned reference to it. Accessing person.apartment causes a runtime crash because the unowned reference points to deallocated memory.
🧠 Conceptual
advanced1:30remaining
Why use unowned references instead of weak references?
Which statement best explains when to use unowned references instead of weak references in Swift?
Attempts:
2 left
💡 Hint
Think about the difference between optional and non-optional references and lifetime guarantees.
✗ Incorrect
Unowned references are non-optional and assume the referenced object will always exist during the reference's lifetime. This avoids optional unwrapping but crashes if the assumption is broken.
❓ Predict Output
expert2:30remaining
Count of items in dictionary with unowned references as keys
What is the number of items in the dictionary after this Swift code runs?
Swift
class Item { let id: Int init(id: Int) { self.id = id } deinit { print("Item \(id) deinitialized") } } var dict: [Unowned<Item>: String] = [:] var item1: Item? = Item(id: 1) var item2: Item? = Item(id: 2) dict[Unowned(item1!)] = "First" dict[Unowned(item2!)] = "Second" item1 = nil print(dict.count)
Attempts:
2 left
💡 Hint
Consider that unowned references do not affect reference counting and dictionary keys remain until explicitly removed.
✗ Incorrect
The dictionary holds unowned references as keys, but setting item1 to nil does not remove the key automatically. The dictionary still contains both keys, so count is 2.