{{announcement.body}}
{{announcement.title}}

Swift Tutorial: Classes vs. Structures

DZone 's Guide to

Swift Tutorial: Classes vs. Structures

Move swiftly — advantages of uses classes and structures in Swift.

· Web Dev Zone ·
Free Resource

swift-on-a-dead-branch


When creating a model in Swift, developers often have to choose between two main options. Structures or Classes, and some times even both. In this blog post, I will be explaining the difference between the two and when and how to use them.

Both Structures and Classes have a lot in common, such as defining properties for storing values, extending to expand their functionality, and conforming to protocols to provide standard functionality. Let us touch on the main difference that sets them apart: Structures are value types, while Classes are reference types.

You may also like: Learn Swift With This Hands-On Tutorial.

What do I mean by this statement? A value type is a type whose value is copied when it is assigned to a variable or constant or passed to a function. Value types are very common in Swift (e.g. integers, booleans, strings, arrays, and dictionaries are all structures). Still confused? Let's see an example in code.

Value Types

struct Person {
    var name = ""
    var age = 0
}

let firstPerson = Person(name: "Tom", age: 35)
var secoundPerson = firstPerson


I just created a structure called Person above and declared a constant firstName an object of Person. Right after that, I declared another variable secoundPerson and the set its value to the value of firstName. Now, this is what is going on behind the scenes.

As we addressed earlier, because structures are value types, a copy of firstName is assigned to secoundPerson. Despite the fact that they have identical values, they are completely different from one another. Now let's test and see if that's true. 

secoundPerson.name = "James"
secoundPerson.age = 40

print("firstPerson's name is \(firstPerson.name)")
print("secoundPerson name is \(secoundPerson.name)")

// Prints "firstPerson's name is Tom"
// Prints "secoundPerson name is James"


To show that secoundPerson is just a copy of firstPerson, I changed the value of  secoundPerson.name, and the values were no longer the same.

Reference Types

Now, classes, on the other hand, are reference types. This means the values are not just copies but a reference of the same existing instance. Let's use the exact same Person file we created earlier, except this time we will make it a class.

class Person {
    var name = ""
    var age = 0
}

let firstPerson = Person()
var secoundPerson = firstPerson


firstPerson.name = "Tom"
secoundPerson.name = "James"
secoundPerson.age = 40

print("firstPerson's name is \(firstPerson.name)")
print("secoundPerson name is \(secoundPerson.name)")

// Prints "firstPerson's name is James"
// Prints "secoundPerson name is James"


Now, in this example, both firstPerson and secoundPerson are connected. As a result, making any changes to one will affect the other. Also, notice that when I created an object of class Person, I didn't add a default initializer because Structures have a builtin initializer. Classes, on the other hand, don't. Also with classes, you can find out if two constants or variables reference the same instance of a class; you cannot do that with structures because structures are value types that just copy one another.

Well, that is my take on Structures vs. Classes, and I hope this article was helpful. Thank you for reading!


Related Articles

Topics:
swift

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}