0
0
Swiftprogramming~10 mins

Debugging memory leaks with Instruments 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 create a strong reference cycle that can cause a memory leak.

Swift
class Person {
    var name: String
    var apartment: Apartment? 
    init(name: String) {
        self.name = name
    }
}

class Apartment {
    var unit: String
    var tenant: Person? 
    init(unit: String) {
        self.unit = unit
    }
}

var john: Person? = Person(name: "John")
var unit4A: Apartment? = Apartment(unit: "4A")
john?.apartment = unit4A
unit4A?.tenant = [1]
Drag options to blanks, or click blank then click option'
Ajohn
Bunit4A
Cself
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the wrong variable to tenant, which does not create a cycle.
Using nil which breaks the reference.
2fill in blank
medium

Complete the code to break the strong reference cycle using a weak reference.

Swift
class Person {
    var name: String
    weak var apartment: [1]?
    init(name: String) {
        self.name = name
    }
}
Drag options to blanks, or click blank then click option'
AAnyObject
BPerson
CApartment
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class type for the weak reference.
Not using weak keyword causing the cycle to persist.
3fill in blank
hard

Fix the error in the closure to avoid a memory leak by capturing self correctly.

Swift
class HTMLElement {
    let name: String
    let text: String?
    lazy var asHTML: () -> String = {
        [1] 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'
Aself
B[weak self]
C[self]
D[unowned self]
Attempts:
3 left
💡 Hint
Common Mistakes
Not using a capture list causing a memory leak.
Using [self] which still strongly captures self.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters and transforms data without causing memory leaks.

Swift
let words = ["apple", "bee", "cat", "dog"]
let filteredLengths = words.reduce(into: [:]) { dict, word in
    if word.count [1] 3 {
        dict[word.[2]()] = word.count
    }
}
Drag options to blanks, or click blank then click option'
A>
Blowercased
Cuppercased
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator which filters incorrectly.
Using uppercased instead of lowercased causing inconsistent keys.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values filtered by a condition.

Swift
let data = ["a": 1, "b": 2, "c": 3, "d": 4]
let result = data.filter { $0.value [1] 2 }
                 .reduce(into: [:]) { dict, pair in
                     dict[[2]] = [3]
                 }
Drag options to blanks, or click blank then click option'
A<
Bpair.key.uppercased()
Cpair.value
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator which filters incorrectly.
Assigning the wrong key or value in the dictionary.