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

There's more...

Now that we have a PersonName struct, let's amend our Person class so that it can use it:

class Person { 

let birthName: PersonName
var currentName: PersonName
var countryOfResidence: String

init(name: PersonName, countryOfResidence: String = "UK") {
birthName = name
currentName = name
self.countryOfResidence = countryOfResidence
}

var displayString: String {
return "\(currentName.fullName()) - Location: \(countryOfResidence)"
}
}

We've added the birthName and currentName properties of our new PersonName struct type, and we initiate them with the same value when the Person object is initiated. Since a person's birth name won't change, we define it as a constant, but their current name can change, so it's defined as a variable.

Now, let's create a new Person object:

var name = PersonName(givenName: "Alissa", middleName: "May", familyName: "Jones") 
let alissa = Person(name: name)
print(alissa.currentName.fullName()) // Alissa May Jones

Since our PersonName struct has value semantics, we can use this to enforce the behavior that we expect our model to have. We would expect to not be able to change a person's birth name, and if you try, you will find that the compiler won't let you.

As we discussed earlier, changing the family name mutates the struct, and so a new copy is made. However, we defined birthName as a constant, which can't be changed, so the only way we would be able to change the family name would be to change our definition of birthName from let to var:

alissa.birthName.change(familyName: "Moon") // Does not compile. Compiler tells you to change let to var 

When we change the currentName to have a new family name, which we can do, since we defined it as a var, it changes the currentName property, but not the birthName property, even though these were assigned from the same source:

print(alissa.birthName.fullName()) // Alissa May Jones 
print(alissa.currentName.fullName()) // Alissa May Jones
alissa.currentName.change(familyName: "Moon")
print(alissa.birthName.fullName()) // Alissa May Jones
print(alissa.currentName.fullName()) // Alissa May Moon

We have used a combination of objects and structs to create a model that enforces our expected behavior.

主站蜘蛛池模板: 南郑县| 汉中市| 鹤峰县| 靖安县| 湟中县| 神农架林区| 垦利县| 额敏县| 高邑县| 潼关县| 浪卡子县| 临高县| 安国市| 福州市| 姚安县| 黔东| 芮城县| 九龙县| 河北省| 葫芦岛市| 安仁县| 墨竹工卡县| 江都市| 黄平县| 蒲城县| 兴业县| 新乡县| 周宁县| 二连浩特市| 萝北县| 曲靖市| 易门县| 石城县| 邢台县| 延川县| 都江堰市| 旺苍县| 潢川县| 永定县| 乾安县| 英超|