EduFlow
Swift Essentials for iOS

Checkpoint: Modelling with Types

Five questions on this section. Answer all of them, then see your score and why each answer is right or wrong.

Question 1 of 5

Expense is a struct with var amount: Decimal. What does this print?

Swift
var list = [Expense(amount: 5)]
var first = list[0]
first.amount = 9
print(list[0].amount)
Question 2 of 5

What does this print?

Swift
enum Sync {
    case idle
    case syncing(progress: Double)
    case failed(String)
}
let state: Sync = .syncing(progress: 0.4)
switch state {
case .idle: print("Idle")
case .syncing(let p) where p > 0.5: print("Almost")
case .syncing: print("Working")
case .failed(let message): print(message)
}
Question 3 of 5

protocol Exportable { func csvRow() -> String } has five conforming types. Every one of them should also get csvLine, which is csvRow() plus a newline, without writing it five times. What do you add?

Question 4 of 5

A final class ReminderScheduler stores var onFire: (() -> Void)? and sets onFire = { self.notify() }. Memory tools show schedulers are never freed. What’s the fix?

Question 5 of 5

load() reads saved expenses and throws if the file is corrupted. What does the user see when that happens?

Swift
let expenses = (try? load()) ?? []

5 questions unanswered