Class vs Struct

Sarathi Kannan
3 min readMar 24, 2023

--

In any mobile application, to store data, track the data flow through out the application is critical part. So choose the correct data type is the important one.

In iOS, to store and model the data we have two options.

  1. Class
  2. Struct

In this article, we cover which one is suitable in different situations.

Before that, need to understand what is about, what are the things are similar and what are the things are different and which use case we need to to use this.

Class:

Class is reference type which means that when we create object of the class, RAM returned the memory address of the object. We can access and modify the class object by passing or using reference of the object. When you pass the class object around the application, different stages of program can share and modify the object.

Struct:

Struct is value type which means that when we create object of the struct, RAM returned the value of the object. We can access and modify the struct object by passing value of the object. When we pass the object to another file, it is not shared a across the application. When you pass the struct object around the application, different stages of program can’t share the modification of the object. Because the copy of the object passed.

Things are common:

  1. Both defined properties to store values. if you want to create properties and behaviours (methods), you can use both of them
  2. Both have initializers to create objects with initial values
  3. Both can be extended
  4. Both can conforming the protocols, but if the protocol have class only, then class only conform those protocols.

Class have additional capabilities that struct don’t have:

  1. Class can inherit another class properties and methods
  2. Class can free up the memory by calling deinitializers
  3. Reference count allows more than one reference for a class object.

When to use class:

  1. When we need Objective-C interoperability, use class. Because If you use an Objective-C API that receives data from our side, those data must be a class because Objective-C doesn’t have structs.
  2. If you need a instance of the object through the application and then need to control object’s identity then class are the solution

When to use struct:

  1. If we need to use common kind of data which is store and use, then you must use struct.
  2. Structs in swift are powerful and have many features, like stored and computed properties and methods, also conforming the protocols. Most of types from foundation and standard library in swift are structs.

Recommendation:

Since class is a reference type it support inheritance, it used mostly for complex use cases. If struct enough to meet your goals then use structs. Use a class when it is need necessarily.

Example code:

Class:

class Fruit {
var name: String
var color: String

init(name: String ,color: String ) {
self.name = name
self.color = color
}
}

var apple = Fruit(name: "Apple", color: "Red")
var orange = apple

print(apple.name) // Apple
print(apple.color) // Red
print(orange.name) // Apple
print(orange.color) // Red

apple.name = "Orange"
apple.color = "Orange"

print(apple.name) // Orange
print(apple.color) // Orange
print(orange.name) // Orange
print(orange.color) // Orange

Struct:

struct Fruit {
var name: String
var color: String

init(name: String ,color: String ) {
self.name = name
self.color = color
}
}

var apple = Fruit(name: "Apple", color: "Red")
var orange = apple

print(apple.name) // Apple
print(apple.color) // Red
print(orange.name) // Apple
print(orange.color) // Red

apple.name = "Orange"
apple.color = "Orange"

print(apple.name) // Orange
print(apple.color) // Orange
print(orange.name) // Apple
print(orange.color) // Red

From the above example you can understand the Struct and Class better.

--

--

Sarathi Kannan

Passionate Mobile Application Developer, iOS, Swift, SwiftUI, Objective C, Flutter