100 Days of SwiftUI

Day 14

2026-05-01

This is the final “fundamentals of Swift” lesson; Day 15 is a review.

Optionals

  • Optionals take care of null referencesi.e. when a variable has no value

How to handle missing data with optionals

  • nil is 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 let syntax
  • 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 let use guard let
  • if let will run if myVar has a value inside; guard let ... else will run if myVar is nil
  • required to return (exit the function) if a guard check fails
  • if the guard finishes (there’s a non-nil value) then it’s considered “unwrapped” and can be used
  • guard is not just for optionals The quiz says differently, perhaps I misunderstood. guard someArray.isEmpty else { return }
  • use if let if you just want to unwrap some optionals, but prefer guard let if 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 returns nil

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.

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) }

🤷‍♀️