Day 3 of 100 days of SwiftUI dives into some more meaty topics, such as complex data types.
Arrays
Arrays seem to behave similarly to JavaScript. We can implicitly declare an array of strings or integers, like this:
let stringArray = ["Hi", "Hello"]
But unlike (vanilla) JavaScript, we can't explicitly define which types are allowed in the array. We can do this in Swift. If I wanted an array of strings and nothing but strings, I could say:
let stringArray: [String?] = []
This will throw an error if I have the audacity of adding any non-string value into the array.
We can access items in arrays using their index.
let firstItem = stringArray[0]
Arrays also have the usual methods like sorted
and reversed
, which do exactly what they sound like they do.
One thing that tripped me up with reversed()
was that it doesn't return an array, but a ReversedCollection
.
This doesn't allow accessing an item using its index, but it does support some array methods, like count.
The biggest difference I found between Swift and JS arrays is that you cannot mix and match types in arrays. So once an array's flavour has been set (String, Int, Doubles, etc.), you cannot add values of a different type to it.
This feels like a good thing, as I'm sure all this type safety will only benefit my sanity down the road. But coming from the wild west of JS, it does take some getting used to.
Objects Dictionaries
Dictionaries, the sidekicks of arrays, are Swift's answer for data that needs to be organized in key:value pairs.
Swift's emphasis on type safety shines here as well. A dictionary can by default accept key:value pairs of all kinds of types. But it can also be created with key:value types explicitly set.
For example, when this code runs:
var ages = [String:Int]()
it creates a dictionary that only accepts keys that are strings and values that are integers.
So, after creating the above dictionary, a line like this would totally work:
ages["tom"] = 123 // 123
but this would trigger an error:
ages["tom"] = "random string" // Error: Cannot assign value of type 'String' to type 'Int?'
Neat!
Another safety dance that Swift allows us to do is declare a default value when accessing a key in a dictionary. This way, if the key doesn't exist, you don't walk away empty handed.
print(ages["alex", default: 333]) // 333
Default values are optional, but Xcode does throw an ugly warning at you if you don't provide a default value.
Sets
Sets are similar to arrays in that they can store multiple values. They also have the usual array methods like count
, and sorted
.
Unlike arrays, however, sets do not remember the order of your items and they do not allow duplicates.
This is intentional, as remembering where your items are placed in an array has a performance cost. Since sets ignore your items positioning, they are much faster to work with than arrays.
Sets can be created by passing in an array of items and will automatically remove duplicates.
let mySet = Set([1, 2, 3, 3])
print(mySet) // [1, 2, 3]
Enums
Enums are interesting tool that Swift has to make your code more predictable and (again) safe.
By creating an enum, you can predefine a list of acceptable values for a variable. If the value doesn't exist, you get an error. This is helpful for cases where there's a fixed number of values that you want to accept.
So if I had a list of food:
enum Food {
case burgers
case pizza
case pasta
}
I can use this list to select values with a lot more control.
This would work:
let myFavouriteFood = Food.burgers
And this would throw an error:
let myFavouriteFood = Food.fries
That's Day 3! Definitely the meatiest lesson so far, but I'm excited to keep going.