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
ToDoItemwith two properties: A string namedtitleand a bool namedchecked - Make a custom
initthat only takes atitleparameter and setscheckedto false - Create a class called
ToDoListwith a variable array calleditemsofToDoItemthat is uninitialized - Make an
initthat takes no parameters and setsitemsto an empty list - Make another
initthat takes an array of items and uses it to initialize the list - Create a class that inherits from
ToDoListcalledDoneList - Override the
initthat takes items and change the parameter so that every item is checked and then callsuper.initto set the items list
Next
The next article will provide exercises for the DeInitialization chapter.