This tutorial aims to provide a comprehensive understanding of what structures (or structs) are in Swift, how they work, and how they differ from classes.
By the end of this tutorial, you will be able to:
- Understand the concept of structures and classes in Swift.
- Differentiate between reference types and value types.
- Implement structures and classes in your Swift programs.
Basic knowledge of Swift programming is recommended to make the most of this tutorial.
Structures, or structs, are one of the basic building blocks in Swift. They allow you to encapsulate related properties and behaviors.
struct Student {
var name: String
var age: Int
}
In this example, Student
is a structure that has two properties, name
and age
.
Classes are similar to structs but come with additional features like inheritance.
class Teacher {
var name: String
var subject: String
init(name: String, subject: String) {
self.name = name
self.subject = subject
}
}
In this example, Teacher
is a class that also has two properties, name
and subject
.
The primary difference between structs and classes lies in the way they are handled in memory. Structs are value types and Classes are reference types.
When you assign a struct to a new variable or pass it to a function, Swift creates a new copy of it.
var student1 = Student(name: "John", age: 20)
var student2 = student1
student2.name = "Jane"
print(student1.name) // Output: John
print(student2.name) // Output: Jane
Even though we change student2.name
to "Jane", student1.name
remains "John".
Classes, on the other hand, are reference types. When you assign a class to a new variable or pass it to a function, Swift creates a new reference to the same instance.
var teacher1 = Teacher(name: "Mr. Smith", subject: "Math")
var teacher2 = teacher1
teacher2.name = "Mr. Johnson"
print(teacher1.name) // Output: Mr. Johnson
print(teacher2.name) // Output: Mr. Johnson
Here, when we change teacher2.name
to "Mr. Johnson", teacher1.name
also changes to "Mr. Johnson".
struct Car {
var make: String
var model: String
}
var car1 = Car(make: "Honda", model: "Civic")
var car2 = car1
car2.model = "Accord"
print(car1.model) // Output: Civic
print(car2.model) // Output: Accord
class Truck {
var make: String
var model: String
init(make: String, model: String) {
self.make = make
self.model = model
}
}
var truck1 = Truck(make: "Ford", model: "F-150")
var truck2 = truck1
truck2.model = "Ranger"
print(truck1.model) // Output: Ranger
print(truck2.model) // Output: Ranger
In this tutorial, we explored structures and classes in Swift. We learned about the difference between value types (structs) and reference types (classes), and how they behave differently when copied or passed around in your code.
Book
with title
and author
properties. Create two instances and observe the behavior when you modify one.Person
with name
, age
, and address
properties. Implement a function that takes a person and changes the address. Observe the behavior for structs and classes.Note: To solve these exercises, you can follow the same pattern as the examples provided in the tutorial.