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

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