November 2016
Intermediate to advanced
480 pages
14h 42m
English
The print() function above is a fine way to print a description of myTown. But a town should know how to describe itself. Create a function on the Town struct that prints the values of its properties to the console. Navigate to your Town.swift file and add the following function definition.
Listing 15.5 Letting Town describe itself (Town.swift)
struct Town {
var population = 5_422
var numberOfStoplights = 4
func printDescription() {
print("Population: \(population);
number of stoplights: \(numberOfStoplights)")
}
}
printDescription() is a method because it is a function that is associated with a particular type. (Recall from Chapter 14 that this is the definition of a method.) Thus far, you have mainly worked ...
Read now
Unlock full access