It feels like it was only 4 days ago that I started the 100 Days of SwiftUI course and here we are, at (surprise!) day 4. Day 4 was relatively short and focused on type annotations. Paul also summarised the complex data types from day 3 and we ended with the second checkpoint. Let’s dive in!
Type annotations
A type annotation in Swift is used when you want to specifically declare what type of data a variable or constant will be. Up until now, we used type inference, because we let Swift determine from the data we put into a variable/constant what type of data type it was.
Paul has a great article on why Swift features type annotations and when you would use it. I recommend you give it a read!
// Type inference
var score = 1.0 // Swift infers from the given data, 1.0, that this is a double.
// Type annotation
var newScore: Double = 1 // Even though I've put in an integer, Swift thinks of it as a Double (1.0) because I used a type annotation.
// This will print 1.0, instead of 1 as I've entered it above.
print(newScore)
// A few more examples of type annotations
let myBlog: String = "codemaverick.dev"
let numberOfBlogPosts: Int // This is a constant that I leave empty. I only tell Swift that whatever value I put in later, will be an integer.
numberOfBlogPosts = 4
print(numberOfBlogPosts)
// ^Important to note. Since numberOfBlogPosts is a constant, once I've assigned 4 as the value, it cannot be changed again later.
SwiftUI checkpoint 2
The second checkpoint was upon me today. The challenge seemed quite simple, but I had quite a bit of thinking to do before I figured it out completely. The assignment:
- Create an array of strings
- Write some code that prints the number of items in the array
- Also write some code that prints the number of unique items in the array
As you may have expected, the first two steps were not that difficult. The third, however, was a bit more tricky. I knew that I could filter out unique items with a set because, as explained in my recap of day 3, a set can only contain unique data. All duplicates are removed. That being said, a set is not an array.
So, it took me a little while, but after connecting the dots, it all made sense: you can create a set and fill it with an array. That train of thought brought me the solution to Checkpoint 2 of the 100 Days of SwiftUI.
// Create an array of strings
var gamesList = [String]()
gamesList.append("Nier: Automata")
gamesList.append("Breath of the Wild")
gamesList.append("Hollow Knight")
gamesList.append("Horizon Zero Dawn")
gamesList.append("Nier: Automata")
gamesList.append("Horizon Zero Dawn")
print(gamesList)
// Print number of items in array
var gamesListCount = gamesList.count
print("The number of games in the games list is \(gamesListCount)")
// Create a set, which will filter out duplicates
var uniqueGamesList = (Set(gamesList))
var uniqueGamesListCount = uniqueGamesList.count
// Print the number of unique items in the array
print("The number of unique games in the games list is \(uniqueGamesListCount)")
And that was it for day 4! I hope you enjoyed it, even though it was a bit short. Is there anything I should’ve done differently for my solution to checkpoint 2? Let me know what you think in the comments below and stay tuned for day 5 tomorrow!
100 Days of SwiftUI – Day 4 – Type Annotations