0
0
Swiftprogramming~10 mins

Why ARC matters for Swift developers - Test Your Understanding

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 to an instance.

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

var person1: Person? = Person(name: "Alice")
var person2 = [1]
Drag options to blanks, or click blank then click option'
Aperson1
Bnil
Cself
DPerson()
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning nil instead of the existing instance.
Creating a new instance instead of referencing the existing one.
2fill in blank
medium

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

Swift
class Person {
    var name: String
    weak var friend: Person? // Avoid strong reference cycle
    init(name: String) {
        self.name = name
    }
}

var alice: Person? = Person(name: "Alice")
var bob: Person? = Person(name: "Bob")
alice?.friend = [1]
Drag options to blanks, or click blank then click option'
Abob
Balice
Cnil
DPerson(name: "Bob")
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning nil instead of the other instance.
Assigning a new instance instead of the existing one.
3fill in blank
hard

Fix the error in the code to prevent a strong reference cycle using unowned reference.

Swift
class Customer {
    let name: String
    var card: CreditCard?
    init(name: String) {
        self.name = name
    }
}

class CreditCard {
    let number: UInt64
    unowned let customer: [1]
    init(number: UInt64, customer: Customer) {
        self.number = number
        self.customer = customer
    }
}

var john: Customer? = Customer(name: "John Appleseed")
john?.card = CreditCard(number: 1234_5678_9012_3456, customer: john!)
Drag options to blanks, or click blank then click option'
AString
BCreditCard
CCustomer
DAnyObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class type for the unowned reference.
Using a value type like String instead of a class.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores word lengths only for words longer than 3 characters.

Swift
let words = ["apple", "bat", "car", "dolphin"]
let lengths = words.reduce(into: [:]) { dict, word in
    if word.count [1] 3 {
        dict[word] = [2]
    }
}
Drag options to blanks, or click blank then click option'
A>
Bword.count
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Assigning the word itself instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase keys and values for words longer than 4 characters.

Swift
let words = ["swift", "code", "apple", "bug"]
let result = words.reduce(into: [:]) { dict, word in
    if word.count [1] 4 {
        dict[[2]] = [3]
    }
}
Drag options to blanks, or click blank then click option'
A>
Bword.uppercased()
Cword
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Swapping keys and values.