O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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."    }

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required