The Swift Programming Language Companion: Initialization

by Lou Franco

This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the ninth 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 Initialization.

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 "14-Initialization"

Exercises for Initialization

At this point, you should have read Initialization 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

For these exercises, we are going to imagine a to do app

In your Playground write code to do the following:

  1. Create a struct called ToDoItem with two properties: A string named title and a bool named checked
  2. Make a custom init that only takes a title parameter and sets checked to false
  3. Create a class called ToDoList with a variable array called items of ToDoItem that is uninitialized
  4. Make an init that takes no parameters and sets items to an empty list
  5. Make another init that takes an array of items and uses it to initialize the list
  6. Create a class that inherits from ToDoList called DoneList
  7. Override the init that takes items and change the parameter so that every item is checked and then call super.init to set the items list

Next

The next article will provide exercises for the DeInitialization chapter.

Never miss an article

Get more articles like this in your inbox.