Chapter    44

Nested Types

When you are working with value types such as enumerations (Chapter 19), structures (Chapter 30), and reference types like classes (Chapter 31), you can nest other types in your type definitions. This means your class definition may include other class definitions, structure definitions, and enumeration definitions. Each of these may contain their own nested types.

For instance, if you wanted to expand the construct of a Person to include health information, you might do something like Listing 44-1.

Listing 44-1. Nested Classes

class Person {    var name: String = "Name"    var age:Int = 0    var health = Health()    func profile() -> String {        return "I'm \(self.name) and I'm \(self.age) years old."    }

Get Swift Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.