Swift Tutorials: Structs vs Classes

beginner
16 min

Swift Tutorials: Structs vs Classes

Welcome to our deep dive into Swift, where we explore the world of Structs and Classes! šŸŽ‰

Swift, a powerful and intuitive programming language, provides two primary ways to create custom data types - Structs (Structures) and Classes. Both Structs and Classes can hold properties and methods, but they differ in some fundamental ways. Let's start by understanding what Structs and Classes are.

What are Structs and Classes?

šŸ“ Structs: Structures are a value type in Swift, similar to primitive data types (Int, Double, etc.). They encapsulate data and functionality, but unlike classes, they are copy-by-value, meaning a new copy is created every time a struct is assigned to a variable.

šŸ“ Classes: Classes are reference types, meaning they are copied by reference. When a class is assigned to a variable, only a reference to the original instance is copied, not the entire instance.

Let's dive deeper into each!

Structs 🌟

Structs are a great choice when you want to create simple data structures that primarily focus on the data they hold.

Properties

A struct can have both computed and stored properties, just like a class.

swift
struct Point { var x: Double var y: Double // Computed property var distanceFromOrigin: Double { return sqrt(x * x + y * y) } }

Initializers

Initializers help create and configure new instances of a struct.

swift
struct Rectangle { var width: Double var height: Double // Initializer init(width: Double, height: Double) { self.width = width self.height = height } }

Copying Structs

Since structs are copy-by-value, a new copy is created every time a struct is assigned to a variable.

swift
var point1 = Point(x: 3.0, y: 4.0) var point2 = point1 point2.x = 5.0 print(point1) // (x: 3.0, y: 4.0) print(point2) // (x: 5.0, y: 4.0)

šŸŽÆ Pro Tip: Because of copy-by-value, structs can help you avoid unexpected side effects when working with multiple instances of the same data structure.

Classes 🌟

Classes are more suitable when you need to create complex data structures that can have methods for managing their own memory and for implementing custom behavior.

Properties

Classes can have stored properties, just like structs.

swift
class Person { var name: String var age: Int }

Initializers

Initializers help create and configure new instances of a class.

swift
class Person { var name: String var age: Int // Initializer init(name: String, age: Int) { self.name = name self.age = age } }

Copying Classes

Since classes are copy-by-reference, when a class is assigned to a variable, only a reference to the original instance is copied, not the entire instance.

swift
var person1 = Person(name: "Alice", age: 25) var person2 = person1 person2.name = "Bob" print(person1) // Person(name: "Bob", age: 25) print(person2) // Person(name: "Bob", age: 25)

šŸŽÆ Pro Tip: Classes can help you manage the lifetime of your objects and handle complex behaviors like delegation and inheritance.

Which to Choose? šŸ¤”

Choosing between a struct and a class depends on your specific needs and the behavior you want your data type to exhibit. Here's a simple guideline:

  • Use a struct when you want a value type that primarily focuses on data.
  • Use a class when you need to create complex data structures that can manage their own memory and handle custom behavior.

Wrapping Up

Now you've learned the basics of Structs and Classes in Swift! Remember, the key difference between the two is that Structs are value types, while Classes are reference types.

Next time, let's delve into Swift's access control levels and learn how to keep our data secure.

Quick Quiz
Question 1 of 1

Which of the following is a value type in Swift?