Hi guys I have a question about structures versus classes. I add two parts of code. First a structure, second a class. In order to change the attributes of pierreDevooght it must be a variable. Contrary to joskeVermeulen whom can be a constant. I guess it has to do with the fact a structure is a value Type and a class is a reference Type. I’m however interested to know why it can be different. Is it because the structure must make a copy?
Code:
struct Person {
let firstName: String
let lastName: String
var address: String
}
var pierreDevooght = Person(firstName: "Pierre", lastName: "Devooght", address: "Vinklaan 7, Merelbeke")
pierreDevooght.address = "Karel De Goedelaan 15, Torhout"
class PersonClass {
let firstName: String
let lastName: String
var address: String
init(firstName: String, lastName: String, address: String){
self.firstName = firstName
self.lastName = lastName
self.address = address
}
}
let joskeVermeulen = PersonClass(firstName: "Joske", lastName: "Vermeulen", address: "Alibabala 14, Merksem")
joskeVermeulen.address = "Travezantelei 55, Schoten"