Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,551
309
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"
 
I am not sure if I understand your question. A struct is indeed a value type and copied when passed around. The result is an immutable value when you assign it to a constant instead of a variable. In your case, if you create a Person struct as a constant (let), then you tell the compiler that this value can never change. If you assign it to a variable, you allow changes to be made. What is happening under the bonnet in terms of copying and memory management is taken care for you.

A class is a reference type and a constant or variable only stores a pointer to an instantiated class (object) in memory. A constant pointer cannot change, so it will always point to the object, whereas a variable pointer can be changed, for instance, to point to a different object. The internal state of the object is not determined by the mutability of the pointer, so you can change its instance variables.
 
  • Like
Reactions: grandM
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"

Long story short, you're right that it has to do with value vs. reference type. When you create a PersonClass, you can create one instance of it that can be passed around and it's the same everywhere. Versus a Struct, when you create a PersonStruct, you pass a copy of it to whatever object wants it. This means any updates to it won't be reflected in instances of it, only in the specific copy you made.

The analogy I like is a website. When you create website (class), it's the same for everyone who goes to it. If you make a change to the site, everyone see's that change. A Struct is similar to printing a screenshot of the website. If you scribble all over the printed page only you see those changes, everyone else see the original copy.

Now, sometimes you DO want to change the value of a struct. You can use the "mutable" keyword that will allow you update properties of the struct. This is great, but once you start getting a ton of "mutable" functions, you might as well of made it a class for simplicity sake. Structs are great, but in most cases I've found using classes makes more sense for day to day apps.
 
  • Like
Reactions: grandM
I am not sure if I understand your question. A struct is indeed a value type and copied when passed around. The result is an immutable value when you assign it to a constant instead of a variable. In your case, if you create a Person struct as a constant (let), then you tell the compiler that this value can never change. If you assign it to a variable, you allow changes to be made. What is happening under the bonnet in terms of copying and memory management is taken care for you.

A class is a reference type and a constant or variable only stores a pointer to an instantiated class (object) in memory. A constant pointer cannot change, so it will always point to the object, whereas a variable pointer can be changed, for instance, to point to a different object. The internal state of the object is not determined by the mutability of the pointer, so you can change its instance variables.
I figured it out. joskeVermeulen is simply the pointer. This pointer is never changed. The property address is changed as it is a variable.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.