Day 12 (part 1 of 4) of 100 Days of Swift

Days 1 to 12 of the “100 Days of Swift” course make up the “Introduction to Swift” section.

Day 12‘s lesson covered optionals, unwrapping, and typecasting. Day 12 is divided into 10 sections with a section 11 being the lesson summary.

The concept of Swift optionals is brand new to me and I was only able to cover sections 1 through 4 today: handling missing data, unwrapping optionals, unwrapping with guard, and force unwrapping.

Thoughts about Day 12

My understanding of Swift optionals is that optionals let us declare a property that holds nothing (or nil) in computer memory. The concept itself is easy enough for me to understand. However, the syntax is such that I need to move through the lesson material at a slow pace in order to better understand and comprehend the material.

I had to work through sections 1 through 4 twice today, which alone required 2 hours 30 minutes of actual study time. My Sunday non-coding activities cut into my Swift study time so I need to continue Day 12 tomorrow.

The background music for today’s study session was streamed from YouTube and titled “The Best Of Mozart – Slowed Down @ 432Hz | 4.5 Hours“.

My Day 12 (part 1 of 2) notes taken from my paper notebook

  1. Handling missing data
    • optionals = store a property that holds nothing or nil.
    • you can make an optional out of any type by placing a question mark ? after the type’s keyword.
    • optionals allow us to represent the absence of some data.
    • example of defining/declaring an optional: var age: Int? = nil
    • assigning the value of nil to a type that doesn’t have the ? after it causes a syntax error.
    • from the Optional reading: there is a link to a video from Brian Voong where he is talking about Swift optionals.
  2. Unwrapping optionals
    • printing a nil String causes a syntax error.
    • to unwrap an optional you must test the optional using the if let syntax.
    • passing an unwrapped function into the print function displays the optional function, which looks like Optional(“text”).
    • the if let syntax with a conditional lets us unwrap (or open) the optional to view its contents to see what’s there.
    • types that are marked as an optional can have a value directly assigned to it.
    • Swift will always unwrap an optional and view the unwrapped optional’s content before allowing you to use the optional.
    • if let can only use the equality-check conditional.
  3. Unwrapping with guard.
    • the guard let syntax is an alternative to the if let syntax.
    • guard let syntax unwraps the optional. If it finds nil then it expects you to exit the function, loop, or condition that the guard let was used in.
    • the difference between guard let and if let is that guard let, after unwrapping the optional, remains usable after the guard let scope closes.
    • use the if let syntax if you only just want to uwrap an optional.
    • use the guard let syntax if you want to specifically check that code conditions are correct before continuing.
  4. Force unwrapping
    • no notes recorded. Did not complete the Test for this section. Will need to pick up tomorrow on this section.

Today’s total study time: 2 hours 30 minutes

100 Days of Swift cumulative study time: 32 hours 30 minutes

[Note: those are actual study time values after subtracting break-time minutes from the Day’s study session]