Mohamed Omar

100 Days of SwiftUI - Day 4

Day 4 of 100 Days of SwiftUI was more of a summary of the previous hefty lesson, which covered the core complex data types: arrays, dictionaries, sets and enums.

The latest lesson did however go over how to explicitly set a type for these data types, rather than rely on Swift's type inference.

For example, given the following code:

var name = "Khaled"

Swift would infer that name is a string, will always be a string, and should never be anything but a string.

This code:

name = 123

would make it very angry. Just furious.

While this code:

name = "Goose"

would make it happy.

This is how vanilla JavaScript works, and it works for my brain. But Swift, like Typescript, does allow explicitly setting the type like so:

var name: String

We can also set a default value for the variable:

var name: String = "Joe"

This of course also applies to complex data types. Here's how we do it for arrays and dictionaries:

var names: [String] = ["John", "Ringo"]
var myDictionary: [String: String] = ["Food": "Sandwich"]

That's basically it in terms of new content for Day 4.