loading...

The Swift Programming Language Companion: Enumerations

loufranco profile image Lou Franco Updated on ・2 min read

The Swift Programming Language Companion (16 Part Series)

1) The Swift Programming Language Companion: Introduction 2) The Swift Programming Language Companion: The Basics 3 ... 14 3) The Swift Programming Language Companion: Optionals 4) The Swift Programming Language Companion: Basic Operators 5) The Swift Programming Language Companion: Strings and Characters 6) The Swift Programming Language Companion: Collection Types 7) The Swift Programming Language Companion: Control Flow 8) The Swift Programming Language Companion: Functions 9) The Swift Programming Language Companion: Functions (Part 2) 10) The Swift Programming Language Companion: Review of Basics to Functions 11) The Swift Programming Language Companion: Review of Basics to Functions (Part 2) 12) The Swift Programming Language Companion: Closures 13) The Swift Programming Language Companion: Enumerations 14) The Swift Programming Language Companion: Structures and Classes 15) The Swift Programming Language Companion: Properties Part 1 16) The Swift Programming Language Companion: Properties Part 2

This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the second article in Section 2 (here is Section 1)

Read each article after you have read the corresponding chapter in the book. This article is a companion to Enumerations.

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.

To add a new page, in Xcode:

  1. Choose File > New > Playground Page
  2. Rename the page to "08-Enumerations"

Exercises for Enumerations

At this point, you should have read Enumerations in The Swift Programming Language. You should have a Playground page for this chapter with code in it that you generated while reading the book.

Exercises

The chapter covers how to declare Enumeration types and use Associated Values.

For these exercises, we are going to imagine a simple media playing app.

In your Playground write code to do the following:

  1. Declare an enumeration called Genre and add cases for jazz, rock, rap, country, and folk.
  2. Write a function called genreName that takes a Genre value and uses a switch to return a string with the name of the genre. So, "Jazz" for .jazz and "Rap" for .rap.
  3. Call genreName with some Genre values.
  4. Extend Genre to implement CaseIterable and write a for in loop to print out all of the genres using Genre.allCases
  5. Change Genre to extend String and change genreName to use genre.rawValue instead of a switch
  6. Make another enumeration called MediaType and add cases for music, podcast, audioBook
  7. Add an associated value for music of type Genre
  8. Write a function called mediaTitle that takes a MediaType value and uses a switch to return a string with the name of the name of the media type. If it's music, make sure to use the associated genre.
  9. Call mediaTitle with some MediaType values.

Next

The next article will provide exercises for the Structures and Classes chapter.

The Swift Programming Language Companion (16 Part Series)

1) The Swift Programming Language Companion: Introduction 2) The Swift Programming Language Companion: The Basics 3 ... 14 3) The Swift Programming Language Companion: Optionals 4) The Swift Programming Language Companion: Basic Operators 5) The Swift Programming Language Companion: Strings and Characters 6) The Swift Programming Language Companion: Collection Types 7) The Swift Programming Language Companion: Control Flow 8) The Swift Programming Language Companion: Functions 9) The Swift Programming Language Companion: Functions (Part 2) 10) The Swift Programming Language Companion: Review of Basics to Functions 11) The Swift Programming Language Companion: Review of Basics to Functions (Part 2) 12) The Swift Programming Language Companion: Closures 13) The Swift Programming Language Companion: Enumerations 14) The Swift Programming Language Companion: Structures and Classes 15) The Swift Programming Language Companion: Properties Part 1 16) The Swift Programming Language Companion: Properties Part 2

Posted on by:

loufranco profile

Lou Franco

@loufranco

Software Engineer on Trello iOS for Atlassian.

Discussion

markdown guide