Chapter    41

Initialization

You initialize an instance to get the instance ready for use. Initialization means you set the starting values for the instance. In Chapter 31, you used the default initializer when you created a new Person instance (see Listing 41-1).

Listing 41-1. Default Initializer

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

In Listing 41-1, you called the default initializer by using the class name followed by parentheses: Person(). Since you supplied default values for the two properties name and age, this worked fine.

Use init() to override the default initializer. ...

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.