DEV Community

Cover image for Reference Type and Value Type in Swift
Saeid Rezaei
Saeid Rezaei

Posted on

Reference Type and Value Type in Swift

In this post, I'm going to talk about one of the basic features of Swift, which exists in most programming languages.

  • Value type
  • Reference type

When working with variables in Swift, it's important to understand the difference between reference types and value types. In this story, we'll explore the key differences between the two and provide examples of when to use each type.

Reference types

such as classes, are variables that store a reference to an object in memory. When one variable is assigned to another variable, they both point to the same object in memory. Any changes made to the object will be reflected in both variables.

Value Types

on the other hand, are variables that store a copy of the object in memory. When one variable is assigned to another variable, they each get their own copy of the object. Any changes made to one variable will not affect the other variable.

Variable and Variable2 are Value type and Variable3 and Variable4 are Reference type

Let's see an example for reference type:

final class User {
    var name: String
    init(name: String) {
        self.name = name
    }
}

var user1 = User(name: "Saeed")
var user2 = user1
user2.name = "David"

print(user1.name) // prints "David"
print(user2.name) // also prints "David"
Enter fullscreen mode Exit fullscreen mode

In this example, we have a class called User with a property called name. We create two variables, user1 and user2, and assign user1 to user2. Since User is a reference type, both variables point to the same object in memory. When we change the name property of user2 to "David", the name property of user1 is also changed to "David".
Here's an example of a value type:

struct Location {
    var x: Int
    var y: Int
}

var location1 = Point(x: 0, y: 0)
var location2 = location1
location2.x = 5

print(location1.x) // prints 0
print(location2.x) // prints 5
Enter fullscreen mode Exit fullscreen mode

In this example, we have a struct called Location with properties x and y. We create two variables, location1 and location2, and assign location1 to location2. Since Location is a value type, both variables have their own copy of the struct. When we change the x property of location2 to 5, the x property of location1 is not affected and remains 0.

In Swift, class is a reference type and struct is a value type.
It's important to understand the difference between reference types and value types and choose the appropriate type for your needs. Reference types are useful when you want multiple variables to refer to the same object in memory, while value types are useful when you want to maintain a separate copy of the object for each variable.
For example, If you ever programmed for UIKit you'll know that they use classes for views rather than structs. but SwiftUI uses struct, there are a couple of reasons for it which you can search and find out the benefits behind this decision.

Conclusion

Reference and Value types in Swift are two important concepts that every developer should be familiar with, they are the fundamental building blocks of the language, and using them correctly will make your code more efficient, readable and maintainable.


Reference Type and Value Type in Swift | by Saeid Rezaeisadrabadi | Towards Dev

When working with variables in Swift, it’s important to understand the difference between reference types and value types.

favicon towardsdev.com

Top comments (1)

Collapse
 
michaeltharrington profile image
Michael Tharrington • Edited

Nice one! Good quick swift tutorial. 🙌