100 Days of SwiftUI
2026-05-01
This is the final “fundamentals of Swift” lesson; Day 15 is a review.
Optionals
- Optionals take care of null references — i.e. when a variable has no value
How to handle missing data with optionals
nilis the value of nothing- optional is identified by a
?e.g.String? - “Schroedinger’s Data Type”
- “empty” is not equal to
nil - unwrap optionals with
if letsyntax - it’s common to name the unwrapped variable the same as the optional variable (and boy is it confusing) – this is called a “shadow copy”
How to unwrap optionals with guard
- instead of
if letuseguard let if letwill run ifmyVarhas a value inside;guard let ... elsewill run ifmyVarisnil- required to
return(exit the function) if aguardcheck fails - if the
guardfinishes (there’s a non-nilvalue) then it’s considered “unwrapped” and can be used guardis not just for optionals The quiz says differently, perhaps I misunderstood.guard someArray.isEmpty else { return }- use
if letif you just want to unwrap some optionals, but preferguard letif you’re specifically checking that conditions are correct before continuing.
How to unwrap optionals with nil coalescing
- coalesce provides a default value if the optional was empty
- use
?? value(OMFG) to indicate what the default value should be if it returnsnil
How to handle multiple optionals using optional chaining
- reading optionals inside optionals
- example
let author = book?.author?.first?.uppercased() ?? "A"- if there is a book
- if there is an author
- if they have a first (name)
- uppercase it
- if none of those are true, return “A”

How to handle function failure with optionals
- optional
try->try? - if you want to know why the function failed, don’t use
try?because it will only tell you it failed - can be used with nil coalescing e.g.
let user = (try? getUser(id: 23)) ?? "Anonymous" - mainly used
- In combination with guard let to exit the current function if the try? call returns nil.
- In combination with nil coalescing to attempt something or provide a default value on failure.
- When calling any throwing function without a return value, when you genuinely don’t care if it succeeded or not – maybe you’re writing to a log file or sending analytics to a server, for example.
Quizzes
- Unwrapping optionals — 7/12
- Unwrapping with guard — 10/12
- Nil coalescing — 10/12
- Optional chaining — 8/12
- Optional try — 6/6
Checkpoint 9
Write a function that accepts an optional array of integers, and returns one randomly. If the array is missing or empty, return a random number in the range 1 through 100.
Write your function in a single line of code.
PuzzleMy solution
I did this two ways because I don’t quite understand what he means by “one line”
let numbers: [Int]? = nil
let randomInteger = numbers?.randomElement() ?? Int.random(in: 1...100)
print(randomInteger)That really wasn’t a function, though? This one is, but it’s not formatted nicely
func rIfunk (nums: [Int]?) -> Int { return nums?.randomElement() ?? Int.random(in: 1...100) }🤷♀️