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.
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
ToDoItemwith two properties: A string named
titleand a bool named
- Make a custom
initthat only takes a
titleparameter and sets
- Create a class called
ToDoListwith a variable array called
ToDoItemthat is uninitialized
- Make an
initthat takes no parameters and sets
itemsto an empty list
- Make another
initthat takes an array of items and uses it to initialize the list
- Create a class that inherits from
- Override the
initthat takes items and change the parameter so that every item is checked and then call
super.initto set the items list
The next article will provide exercises for the DeInitialization chapter.