loading...

The Swift Programming Language Companion: Closures

loufranco profile image Lou Franco Originally published at app-o-mat.com ・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 first 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 Closures.

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 "07-Closures"

Exercises for Closures

At this point, you should have read Closures 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 create and call closures and the concept of capturing.

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

In your Playground write code to do the following:

  1. Declare a Boolean variable called isPlaying and set it to false.
  2. Declare a constant called play that is a closure type that takes no arguments and returns Void. Initialize it with a closure that prints the word "Play" and sets isPlaying to true when called.
  3. Call play. Print isPlaying (it should be true)
  4. Make a constant called stop which is like play but prints "Stop" and sets isPlaying to false
  5. Call stop and print isPlaying
  6. Under isPlaying, Declare an optional String variable called currentSong
  7. Add a String argument to play called song and set currentSong to it inside the closure.
  8. Update your call to play pass a song argument
  9. Print currentSong after calling play
  10. Declare a playList array of Strings and initialize it with 5 songs.
  11. Sort the list by the length of the song name

Next

The next article will provide exercises for the Enumerations 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 Jun 6 by:

loufranco profile

Lou Franco

@loufranco

Software Engineer on Trello iOS for Atlassian.

Discussion

markdown guide