Hey guys, this is Ashank again, and today we are talking about structures. If you missed the last post about Classes and Objects, you can go back to the first post in the Swift Blog section. Let’s get started!
What Are Structures?
A structure is very similar to a class, but it has a few key differences that set it apart from a class. The most major difference is that structures use value types, while classes use reference types. Let’s delve into what these words mean.
Value Types vs. Reference Types
To make things clear, structures use value types and classes use reference types. What do these words mean? Well, let’s start off with classes since we have discussed them in the past section.
Classes use reference pointers. What does this mean? Well, when we create an object of any given class, a piece of memory is made by the Java compiler. The name that we give to the object is a reference pointer, or just an arrow pointed to that memory. So, when we make another object equal to the first object, we are simply just adding another arrow to that same memory. Let’s put this in terms of a picture.
When we first create the object, we have a name, the pointer, and the memory it points to.
When we make a second object and make it equal to the first object, all that simply happens is this:
So, when we change a property of an object, the memory is being changed. Therefore, all objects that are equal to each other will change if one of the objects is changed. This is the primary property of a class.
Now, let’s move on to value types with structures. In classes, if we set two objects equal to each other, then both of them would change if any attribute of one of the objects were changed. However, structures use value types. In other words, when you set an object of one structure equal to another object of a structure, a whole new copy of the memory is created. Let’s explain this in terms of a picture.
Notice that there is no pointer in this instance of a structure. Only a blob of memory is there. If we make another instance of the structure and set it equal to the first instance, something like this will happen:
So as you can see, structures are used to make whole new memories of that same data, while classes are used to make references to the same data.
Let’s test this out in XCode.
Classes and Structures in XCode
Once again, you can open a playground in XCode to test the concepts discussed above. First, we will want to make a class, just like last time. The class that we will be using is the Person class. You can go back to the previous post in this series if you do not remember what constitutes a class.
Here we have an object of the Person class named personOne. Now let’s make another object that equals personTwo.
As you can see on the right-hand console, personTwo has all the same properties of personOne. Now, we will change a property of personOne and see what effect it has on personTwo.
As you can see, we didn’t change any attribute of personTwo, but it automatically got changed anyways. How? Well, remember that the names of the Person objects are simply arrows pointing to a single piece of memory. When we changed personOne’s name to Sam, we changed that single piece of memory. As a result, all arrows, or instances of personOne equal to each other in this case, will point to that one piece of memory, giving us the same result for both personOne and personTwo.
Now that we have seen reference types in action with classes, let’s see how value types compare with structures. Structures have the same syntax as classes, so all we have to do is simply replace the “class” keyword with the “struct” keyword and check what are results are.
Immediately you can notice that personTwo does not have the same firstName as personOne anymore. Why? Because when we set personTwo equal to personOne, a whole new copy of memory was created. That’s why when we change personOne’s firstName, personTwo’s name is not affected.
This is a subtle, but very useful distinction between classes and structures. If used correctly in context, structures and classes can be equally very powerful. Depending on the conditions of you want your objects to be, you can choose to make it a part of classes or structures. For example, if you wanted several objects equal to each other but wanted to change one attribute out of hundreds, then you would use structures. If you wanted several objects to automatically change themselves when only one object was edited, then you would use classes. If used correctly, structures and classes are super powerful and will be very useful to you when you start iOS Development.
That’s it from me this time. I hope you learned a lot from this post! Please feel free to leave comments and suggestions about the next post. Goodbye for now!