Class VS Struct in Swift

Ahmed Menaim
5 min readJan 4, 2021

In programmer’s life there is almost no code without structs or classes so while navigating in any code you need to be aware of the differences between them.

The first and the most important thing you need to know is that class is a reference type and struct is value type. You introduce structures with struct keyword and classes with the class keyword.

Let’s start with syntax highlighted for each:

1- Struct:

struct myStruct {  
// Definition
}

2- Class:

class myClass { 
// Definition
}

Common Traits for both classes and structs:

  1. Have properties and store values
  2. Both can adopt from protocols. “Protocols will be explained later”
  3. Have functions
  4. Have initializes

But classes have more traits than structs:

  1. Inheritance from other classes.
  2. De-initializes to enable the instance to free the resources
  3. Allows more than one reference to a class instance

Suppose you are picking a movie to watch and this movie has some properties included name, resolution, frame rate & quality:

struct Resolution {    
var height = 1280
var width = 720
}
class Movie {
var name: String?
var fullHD = false
var resolution = Resolution()
var frameRate = 0.0
}

You have created an instance from Resolution struct to be able to use it in your class.

Note: Not all of these numbers are true, It’s just to have some fun while using our example.

Now you need to check the value of the resolution or the value of any attribute:

let movie = Movie()
print("Movie height is \(movie.resolution.height)")
print("Movie width is \(movie.resolution.width)")

Output:

Movie height is 1280
Movie width is 720

Now as anyone wanna watch any movie, the first thing he asked about is the resolution, that’s why it was the first thing to check.

let myResolution = Resolution()
print("Movie height is \(myResolution.height)")
print("Movie width is \(myResolution.width)")

Output:

Movie height is 1280
Movie width is 720

and of course you will ask about the movie name:

print ("Movie name is \(movie.name)")

but unfortunately you won’t get any name for the movie as you didn’t pick any movie yet and that’s why you will face a warning like the image:

and this is because your attribute name is optional, if you need to understand more about optional in swift check this: Optionals in swift

movie.name = "The Pursuit of Happyness"
print ("Movie name is \(movie.name!)")

Output: Movie name is The Pursuit of Happyness

Now let’s set all of the properties of our movie:

let movie = Movie()

movie.name = "The Pursuit of Happyness"
print ("Movie name is \(movie.name!)")
//Output: Movie name is The Pursuit of Happyness

movie.resolution.width = 1080
print("Movie width is \(movie.resolution.width)")
//Output: Movie width is 1080

movie.resolution.height = 1920
print("Movie height is \(movie.resolution.height)")
//Output: Movie height is 1920

movie.frameRate = 60.0
print("video frame rate is \(movie.frameRate)")
//Output: Movie frame rate is 60.0

movie.fullHD = true
print("Is movie quality is HD? \(movie.fullHD)")
//Output: Is movie quality is HD? true

And as you saw, you can access and update each property belongs to the class by just put . after the instance to be able to access its properties and update it which means you can update any thing about your movie “fullHD, resolution, etc…”

Now you are confused and not sure if class is a reference type and struct value type, so you decided to check it by yourself starting by struct:

movie.resolution.width = 1080
print("movie width is \(movie.resolution.width)")
//Output: movie width is 1080

movie.resolution.height = 1920
print("movie height is \(movie.resolution.height)")
//Output: movie height is 1920

var resolution = Resolution()
resolution.width = 2800
print("width is \(resolution.width)")
//Output: video width is 2800

resolution.height = 1000
print("height is \(resolution.height)")
//Output: height is 1000

In the previous block of code you decided to create an instance called resolution from struct Resolution and at the same time you’ve changed the values of the property resolution in the instance movie, at the first 4 lines and after printing these values you discovered that each one has its own value and each one is separate from the other one in spite of being both under the same name which is resolution, but they are indecent from each other which proves that struct is a value type.

After checking struct, you are wondering are classes reference types or not:

var newMovie = movie

newMovie.name = "The fault in our stars"
print("movie name is \(movie.name!)")
//Output: movie name is The fault in our stars

print("newMovie name is \(newMovie.name!)")
//Output: newMovie name is The fault in our stars

In the previous code you thought of creating a new instance called newMovie and made it equal to your old instance movie, then you tried to give your newMovie a name in the second line and which done is printing the name of your old movie and newMovie which are exactly the same in spite of old instance movie’s name “The Pursuit of Happyness” but now it has changed without doing anything to “The fault in our stars” which makes you now sure that classes are reference types not value types.

Identity Operators:

As you are sure now that classes are reference types so while trying to compare its values you cannot use == or != as usual but you can use === for equality or !=== for inequality:

var newMovieAgain = movie
if newMovie === newMovieAgain {
print("Two instances are from the same main instance (movie)")
}
else {
print("They are different")
}

Output: Two instances are from the same main instance (movie)

In the previous code you were just comparing between your two new instances which are both from the same main instance “video” that’s why you have the output which says that they are identical.

The last thing you decided to know to get out of your movies box is pointers which is so easy to use in Swift comparing it with the different languages as in some other languages you need to put * to indicate a reference but in Swift it’s treated like any variable or constant and already apple provides you with some pointers that you can use and you can check them from here.

Now you are almost done and you can check your movie at anytime with any quality you need.

Thanks for reading️! Help spread the word & wait for the coming articles…

Have questions, suggestions, comments, or ideas for upcoming blog posts? Contact me on LinkedIn or write a comment! You can also follow me on GitHub & check my Stackoverflow answers and questions.

--

--

Ahmed Menaim

iOS / Software Engineer / Building iOS Apps / Spread knowledge