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:
- Choose File > New > Playground Page
- 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:
- Create a struct called
ToDoItem
with two properties: A string namedtitle
and a bool namedchecked
- Make a custom
init
that only takes atitle
parameter and setschecked
to false - Create a class called
ToDoList
with a variable array calleditems
ofToDoItem
that is uninitialized - Make an
init
that takes no parameters and setsitems
to an empty list - Make another
init
that takes an array of items and uses it to initialize the list - Create a class that inherits from
ToDoList
calledDoneList
- Override the
init
that takes items and change the parameter so that every item is checked and then callsuper.init
to set the items list
Next
The next article will provide exercises for the DeInitialization chapter.