8.2 Time Class: Default Initializers and Property Observers

Our first example presents a simple Time class that represents the time of day. We use this class to introduce default initializers and property observers. The example consists of two files—Time.swift (Fig. 8.1) containing class Time and main.swift (Fig. 8.2) containing code that tests class Time’s features. The example’s output appears in Fig. 8.2.

 1   // Fig. 8.1: Time.swift (TimeClass.xcodeproj) 2   // Time class with default initializer and property observers 3   public class Time { 4       // an hour value in the range 0-23 5       public var hour: Int = 0 {                                    6           willSet {                                                 ...

Get Swift™ for Programmers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.