This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the fourth 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 Properties.
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:
At this point, you should have read Properties 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.
The chapter covers how to declare Properties inside of Structures and Classes. For Part 1, we're going to stick to the more common parts, stored properties, computed properties, and Type properties.
For these exercises, we are going to imagine a simple media playing app.
In your Playground write code to do the following:
Genreand add cases for
Artiststruct with stored properties
Songstruct with stored properties
birthDateand today’s date.
Libraryand add a Type property called
songsthat is an array of
Songstructs. Initialize it with a few values.
To find today’s date:
let today = Date()
To find the number of seconds between two dates:
let seconds = today.timeIntervalSince(birthDate)
The next article will look at the more advanced usage of properties.