官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 内江市| 辉南县| 汉阴县| 宁明县| 玉溪市| 和林格尔县| 宜兴市| 曲松县| 黄冈市| 绥棱县| 和顺县| 庆城县| 民勤县| 章丘市| 英吉沙县| 清新县| 平舆县| 平顶山市| 兴仁县| 翁牛特旗| 广西| 葵青区| 云浮市| 新竹市| 肥东县| 天津市| 双柏县| 肇源县| 兖州市| 永胜县| 元朗区| 乐亭县| 沁源县| 安吉县| 麻江县| 通海县| 博罗县| 衡阳市| 洛宁县| 松原市| 木里|