DEV Community

Lou Franco
Lou Franco

Posted on • Updated on • Originally published at app-o-mat.com

The Swift Programming Language Companion: Review of Basics to Functions

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.

Set up a reading environment

If you are jumping around these articles, make sure you read the Introduction to see my recommendation for setting up a reading environment.

Review

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.

This is practice

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.

Exercises

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],
]
Enter fullscreen mode Exit fullscreen mode

In your Playground write code to do the following

  1. Add some songs to the array. Follow the same key structure.
  2. Make a function that takes the songs as a parameter and returns the number of songs in the array.
  3. 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?

    This return 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:

    getTitle(song: songs[0])

  5. 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 nil, return 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)

Next:

We'll continue with more review of these chapters in the next article.

Top comments (0)