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!
Arrays
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")
print(animeShows)
// Prints: ["Fly me to the Moon", "Spy x Family", "Fullmetal Alchemist"]
print(animeShows.sorted())
// Print ["Fly me to the Moon", "Fullmetal Alchemist", "Spy x Family"]
print(animeShows.count)
// Prints 3, because the array contains 3 shows.
animeShows.removeAll()
// Removes all items from the array.
print(animeShows.count)
// 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)
print(footballTable["Real Madrid", default: 0])
// This will print 1, because the value assigned to Real Madrid is 1.
Sets
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
print(actors)
// instead of append, you use insert with a set
actors.insert("Wesley Snipes")
actors.insert("Tom Hanks")
actors.insert("Will Smith")
Enum
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!
100 Days of SwiftUI – Day 3 – Complex Data Types