0
0
Swiftprogramming~10 mins

Weak references to break cycles in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a weak reference to avoid a strong reference cycle.

Swift
class Person {
    var name: String
    weak var friend: Person? // [1]
    init(name: String) {
        self.name = name
    }
}
Drag options to blanks, or click blank then click option'
Aweak
Bstrong
Cunowned
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'strong' instead of 'weak' causes a strong reference cycle.
Forgetting to mark the property as optional when using 'weak'.
2fill in blank
medium

Complete the code to declare an unowned reference to avoid a strong reference cycle when the referenced object is expected to always exist.

Swift
class Customer {
    var name: String
    [1] var card: CreditCard?
    init(name: String) {
        self.name = name
    }
}
Drag options to blanks, or click blank then click option'
Aweak
Bunowned
Cstrong
Dlazy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'weak' when the reference should be non-optional.
Using 'strong' which causes a strong reference cycle.
3fill in blank
hard

Fix the error in the closure capture list to avoid a strong reference cycle.

Swift
class HTMLElement {
    let name: String
    let text: String?
    lazy var asHTML: () -> String = { [[1] self] in
        if let text = self.text {
            return "<\(self.name)>\(text)</\(self.name)>"
        } else {
            return "<\(self.name) />"
        }
    }
    init(name: String, text: String? = nil) {
        self.name = name
        self.text = text
    }
}
Drag options to blanks, or click blank then click option'
Aweak
Bstrong
Cunowned
Dlazy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'strong' causes a strong reference cycle.
Using 'weak' requires optional unwrapping inside the closure.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores weak references to Person objects.

Swift
var people: [String: [1] Person] = [:]

func addPerson(name: String, person: Person) {
    people[name] = [2] person
}
Drag options to blanks, or click blank then click option'
AWeakReference
Bweak
CStrongReference
Dunowned
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use 'weak' directly in dictionary value type causes errors.
Using 'unowned' incorrectly in this context.
5fill in blank
hard

Fill all three blanks to complete the WeakReference wrapper class that holds a weak reference to an object.

Swift
class WeakReference<T: AnyObject> {
    [1] var value: T?
    init(value: T) {
        self.[2] = value
    }
    deinit {
        print("WeakReference deinitialized")
    }
}

let person = Person(name: "Alice")
let weakRef = WeakReference(value: [3])
Drag options to blanks, or click blank then click option'
Aweak
Bvalue
Cperson
Dunowned
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring the property as strong causes retain cycles.
Using 'unowned' here can cause crashes if the object is deallocated.