100 Days of SwiftUI Day 3

100 Days of SwiftUI – Day 3 – Complex Data Types

In day 3 of the 100 Days of SwiftUI, we tackle complex data types. Complex data types are constantly used when programming in Swift, so it’s import to know what sort of data type to use and when to use it. Let’s dive in!


Want to store lots of data in the same place? Look no further, arrays have got you covered. An array is it’s own data type like a string or an integer. However, unlike the latter data types, an array can actually store multiple values. You can create a list of names, numbers, doubles and so on. An important fact is that, as touched on in my day 2 recap, Swift is a type-safe language. This means that you can only store one data type in an array. For example, if you have an array containing the names of students, you cannot add an integer to it.

// Create an empty array.
var animeShows = [String]() 

// Add data to array.
animeShows.append("Fly me to the Moon")
animeShows.append("Spy x Family")
animeShows.append("Fullmetal Alchemist")

// Prints: ["Fly me to the Moon", "Spy x Family", "Fullmetal Alchemist"]

// Print ["Fly me to the Moon", "Fullmetal Alchemist", "Spy x Family"]

// Prints 3, because the array contains 3 shows.

// Removes all items from the array.

// Prints 0, because the array has been cleared

Data dictionaries

Data dictionaries are not so dissimilar from arrays. They are both ways of storing lots of data in a single variable. The difference lies in how a data dictionary stores the data: it uses a key to identify an item. An array does not use keys. Instead, items in an array are stored in the order that they are entered.

The benefit of the way a data dictionary stores data is that the data is easily identifiable.

var superhero1 = [
    "Hero Name": "Spider Man",
    "Real Name": "Peter Parker",
    "Super Power": "Web slinging"

// Creates a data dictionary with a string as key, int as value
var footballTable = [String: Int]()
footballTable["Real Madrid"] = 1
footballTable["FC Barcelona"] = 2
footballTable["Atletico Madrid"] = 3
footballTable["Sevilla"] = 4


print(footballTable["Real Madrid", default: 0])
// This will print 1, because the value assigned to Real Madrid is 1.


Just like arrays and data dictionaries, a set stores a lot of data in a single variable. This time, there are two important differences: a set is not ordered and it cannot contain any duplicates. A set is useful, because it is orders of magnitudes quicker to search through than an array. Paul Hudson has a short but informative article on why sets are different. So, if you ever need to store a lot of data that doesn’t need to be sorted, then skip the array and use a set instead.

// Create a set
var actors = Set([
    "Denzel Washington",
    "Tom Cruise",
    "Nicolas Case",
    "Samuel L Jackson"

// calling print will output them in a random order

// instead of append, you use insert with a set
actors.insert("Wesley Snipes")
actors.insert("Tom Hanks")
actors.insert("Will Smith")


Last up today are enums. An enum is incredibly useful. In short, they allow you to set up easy to understand and safe names for values. Let’s say I’d like a day of the week to be selected in my program. A week always has the same 7 days. The same goes for the months of the year. These examples are excellent choices to use enums for, because selecting a day or month is easier than having to type them in a string variable every time.

// Create an enum
enum Weekday {
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday

// a shorter way to declare the same enum
enum WeekdayShort {
    case monday, tuesday, wednesday, thursday, friday

var day = Weekday.monday
day = Weekday.tuesday
day = Weekday.friday

That was it for day 3 of he 100 Days of SwiftUI. I’m already feeling like I’m learning very useful basics of Swift and the pacing is great. Onwards and upwards we go!


Hi! My name is Darryl and this is my personal blog where I write about my journey as I learn programming! You'll also find articles about other things that interest me including games, tech and anime.

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

100 Days of SwiftUI – Day 85

100 Days of SwiftUI – Day 13 – Protocols

100 Days of SwiftUI – Day 94

100 Days of SwiftUI – Day 59