0
0
Swiftprogramming~20 mins

Why ARC matters for Swift developers - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift ARC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift code involving ARC?
Consider the following Swift code that uses classes and ARC. What will be printed when the code runs?
Swift
class Person {
    let name: String
    init(name: String) {
        self.name = name
        print("\(name) is initialized")
    }
    deinit {
        print("\(name) is being deinitialized")
    }
}

var person1: Person? = Person(name: "Alice")
var person2 = person1
person1 = nil
print("person1 is set to nil")
person2 = nil
print("person2 is set to nil")
A
Alice is initialized
person1 is set to nil
Alice is being deinitialized
person2 is set to nil
B
Alice is initialized
Alice is being deinitialized
person1 is set to nil
person2 is set to nil
C
person1 is set to nil
person2 is set to nil
Alice is initialized
Alice is being deinitialized
D
Alice is initialized
person1 is set to nil
person2 is set to nil
Alice is being deinitialized
Attempts:
2 left
💡 Hint
Think about when ARC releases the object based on strong references.
🧠 Conceptual
intermediate
1:30remaining
Why does ARC prevent memory leaks in Swift?
Which statement best explains why ARC helps prevent memory leaks in Swift?
AARC automatically frees memory when there are no more strong references to an object.
BARC requires manual calls to free memory after using an object.
CARC duplicates objects to avoid memory leaks.
DARC disables reference counting for value types.
Attempts:
2 left
💡 Hint
Think about how ARC tracks object usage.
🔧 Debug
advanced
2:30remaining
Identify the cause of the memory leak in this Swift code
This Swift code creates a memory leak. What causes the leak?
Swift
class Person {
    let name: String
    var apartment: Apartment?
    init(name: String) { self.name = name }
    deinit { print("Person \(name) is deinitialized") }
}

class Apartment {
    let unit: String
    var tenant: Person?
    init(unit: String) { self.unit = unit }
    deinit { print("Apartment \(unit) is deinitialized") }
}

var person: Person? = Person(name: "Bob")
var apartment: Apartment? = Apartment(unit: "4A")
person!.apartment = apartment
apartment!.tenant = person

person = nil
apartment = nil
AStrong reference cycle between Person and Apartment causing neither to be deinitialized.
BThe classes use value types instead of reference types.
CThe variables person and apartment are not set to nil properly.
DThe deinit methods are missing calls to free memory.
Attempts:
2 left
💡 Hint
Look at how the two classes reference each other.
📝 Syntax
advanced
1:30remaining
Which option correctly declares a weak reference in Swift?
You want to avoid a strong reference cycle by declaring a weak reference. Which of the following is the correct syntax?
Avar delegate weak: SomeDelegate?
Bvar weak delegate: SomeDelegate?
Cweak var delegate: SomeDelegate?
Dvar delegate: weak SomeDelegate?
Attempts:
2 left
💡 Hint
The weak keyword comes before var or let.
🚀 Application
expert
3:00remaining
How many times is the deinit method called in this Swift ARC example?
Given the following Swift code, how many times will the deinit method be called when the program finishes?
Swift
class Node {
    let value: Int
    var next: Node?
    init(value: Int) { self.value = value }
    deinit { print("Node \(value) is deinitialized") }
}

var first: Node? = Node(value: 1)
var second: Node? = Node(value: 2)
var third: Node? = Node(value: 3)

first!.next = second
second!.next = third
third!.next = first

first = nil
second = nil
third = nil
A2 times
B0 times
C3 times
D1 time
Attempts:
2 left
💡 Hint
Consider the strong reference cycle created by the next pointers.