Object–Oriented Programming with Swift 2
上QQ阅读APP看书,第一时间看更新

Declaring stored properties

When we design classes, we want to make sure that all the necessary data is available to the methods that will operate on this data; therefore, we encapsulate data. However, we just want relevant information to be visible to the users of our classes that will create instances, change values of accessible properties, and call the available methods. Thus, we want to hide or protect some data that is just needed for internal use. We don't want to make accidental changes to sensitive data.

For example, when we create a new instance of any superhero, we can use both its name and birth year as two parameters for the constructor. The constructor initializes the values of two properties: name and birthYear. The following lines show a sample code that declares the SuperHero class:

class SuperHero {
    var name: String
    var birthYear: Int
    
    init(name: String, birthYear: Int) {
        self.name = name
        self.birthYear = birthYear
    }
}

The next lines create two instances that initialize the values of the two properties and then use the print function to display their values in the Playground:

var antMan = SuperHero(name: "Ant-Man", birthYear: 1975)
print(antMan.name)
print(antMan.birthYear)
var ironMan = SuperHero(name: "Iron-Man", birthYear: 1982)
print(ironMan.name)
print(ironMan.birthYear)

The following screenshot shows the results of the declaration of the class and the execution of the lines in the Playground:

We don't want a user of our SuperHero class to be able to change a superhero's name after an instance is initialized because the name is not supposed to change. There is a simple way to achieve this goal in our previously declared class. We can use the let keyword to define an immutable name stored property of type string instead of using the var keyword. We can also replace the var keyword with let when we define the birthYear stored property because the birth year will never change after we initialize a superhero instance.

The following lines show the new code that declares the SuperHero class with two stored immutable properties: name and birthYear. Note that the initializer code hasn't changed, and it is possible to initialize both the immutable stored properties with the same code:

class SuperHero {
 let name: String
 let birthYear: Int
    
    init(name: String, birthYear: Int) {
        self.name = name
        self.birthYear = birthYear
    }
}

The next lines create an instance that initializes the values of the two immutable stored properties and then use the print function to display their values in the Playground. Then, two lines of code try to assign a new value to both properties and fail to do so because they are immutable properties:

var antMan = SuperHero(name: "Ant-Man", birthYear: 1975)
print(antMan.name)
print(antMan.birthYear)

antMan.name = "Batman"
antMan.birthYear = 1976

The Playground displays the following two error messages for the last two lines, as shown in the next screenshot:

  • Cannot assign to property: 'name' is a 'let' constant
  • Cannot assign to property: 'birthYear' is a 'let' constant

Tip

When we use the let keyword to declare a stored property, we can initialize the property, but it becomes immutable—that is, a constant—after its initialization.