Hacking With SwiftUI, Day 3

Here we are at Day 3 of Hacking With SwiftUI. Yesterday’s lesson was about Booleans and string interpolation, with a quick checkpoint thrown in at the end to make sure I was actually retaining the information from Day 1 and 2. Today’s lesson, by contrast, dives down a little deeper into more complex topics.

Arrays

Arrays are just a method of storing multiple pieces of data simultaneously. In Paul’s example, he uses it to store the names of the members of The Beatles, a series of temperatures, and the infamous numbers from Lost. You can read back data from a specific position in the array – called an “index” – and you can append data to the end of an array as long as it’s a variable. The untouchable nature of constants continues on, I guess.

Apparently, there are no restrictions on adding the same value multiple times, so you could have 27 “Ringos” in your array of Beatles members, but I assume there’s some solution to that so you don’t have duplicate data.

It seems noteworthy that you’re only allowed to add the same kind of data to an array, and you can’t use two different kinds of arrays together. So if I have an array containing nothing but strings, and an array containing nothing but integers, I can’t smash them together in code. Paul has a helpful example of this:

let firstBeatle = beatles[0]
let firstNumber = numbers[0]
let notAllowed = firstBeatle + firstNumber

The fact that you can do work on the data in an array with a simple command like array.count or array.sorted already seems useful, but I’m wondering why you wouldn’t want to just store them in the sorted format instead. I’m sure there’s some reason I will learn later on, but so far it’s super easy to modify or get basic info about the contents of an array and that seems helpful!

I scored 6/6 on the mini-test about Arrays, so apparently I absorbed something.

Dictionaries

TURNS OUT dictionaries are the solution I was looking for. This is where you can set all the magical key/value pairs that I’ve heard about forever, assign default values, and perform the same kind of operations on them that you can on arrays.

Nifty stuff!

One slight area of concern is that there appear to be even more rules about parentheses, brackets, commas, and colons now, which for someone who likes to read fast and try stuff out is going to be a painful lesson. I foresee many broken lines of code because I put a bracket instead of a brace, etc.

5/6 on the test for this one, so not too shabby.

Unfortunately, I did much worse on the “Dictionary Default Values” test, only scoring 8/12. Sure enough, my concern about reading too fast turned out to be true. At least two that I got wrong were about missing colons or trying to mix and match strings and integers. Ugh.

Sets and Enums

Luckily, I bounced back on the Sets and Enum tests. 6/6 on both! Enums seem to be very powerful since you’re strictly limiting the potential values that can be assigned to a var or constant. Paul Hudson quietly mentions in the additional reading at the end of today’s lesson:

As you progress, you’ll learn how Swift lets us add more functionality to enums – they are more powerful in Swift than in any other language I have seen.

Just telling me enough to keep me interested!

Tomorrow’s lesson is a short one, just type annotations and then a second checkpoint. My weekend is going to be hectic, but I’m hoping I can keep my streak alive.