This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple.
We've covered the basics up to functions, and now I'll provide exercises that review topics from all of those chapters.
If you are jumping around these articles, make sure you read the Introduction to see my recommendation for setting up a reading environment.
This is a good point to review because you actually know enough to make some interesting functions. It is not yet enough to jump into apps, because the iOS Frameworks use some of the more advanced Swift features that we haven't learned yet.
At this point, I would recommend staying away from App Tutorials until we have more of the fundamentals.
To recap, we learned about the basic types (numbers, strings, bools), the operations you could do on them, optionals, control flow, collection types (arrays, sets, and dictionaries), and functions.
As a reminder -- if you are a coder in other languages, these exercises will seem very simple. You might be tempted to just look at them and think, "I know how to do this". And, maybe you do.
But, just do them anyway in a Playground. The intent is to get automatic with Swift syntax so that you can write it as fast as you think without needing to look up simple things all of the time.
If you already code, this won't take long. If these articles are your introduction to programming, then you should be doing these exercises more than once.
For this chapter, we're going to write code inspired by the Music app.
Copy this into your playground
let songs: [[String: Any]] = [ ["title": "Shake it off", "artist": "Taylor Swift", "year": 2014], ["title": "Java", "artist": "Al Hirt", "year": 1963], ["title": "Galaxy Song", "artist": "Monty Python", "year": 1983], ["title": "Oh, Mojave", "artist": "The Ruby Suns", "year": 2007], ["title": "Fast As You Can", "artist": "Fiona Apple", "year": 1999], ["title": "Keyboard", "artist": "Pascal", "year": 2018], ]
In your Playground write code to do the following
Make a function that takes the songs as a parameter and an index number (n) and returns the Nth song in the array.
func getSong(songs: [[String: Any]], n: Int) -> [String: Any]
4. Make a function that takes a single song dictionary (
[String: Any]) and returns the title.
func getTitle(song: [String: Any]) -> String?
String? because there is nothing enforcing that the title key is there or that it is a string. You can use
as? String on a variable to try to convert it. This will give you the string or nil.
You should be able to call it like this:
Make a function that takes a single song dictionary (
[String: Any]) and a year and returns
true if the song is from that year.
func isSongInYear(song: [String: Any], year: Int) -> Bool
When you get the year, if you get
false from the function.
6. Make a function that takes the entire
songs array and a year and returns the songs from that year as an array of song dictionaries.
7. Make a function that takes the entire
songs array and two years and returns the songs that are between (and including) those years.
func getSongsBetween(songs: [[String: Any]], startYear: Int, endYear: Int)
Call it like this:
getSongsBetween(songs: songs, startYear: 1981, endYear: 2007)
We'll continue with more review of these chapters in the next article.