Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources
Introduction to Structs and Classes in Swift

Introduction to Structs and Classes in Swift

What are Structs and Classes?

Structs and classes are fundamental building blocks in Swift programming. Both are used to create complex data types by grouping related properties and methods. While they share some similarities, there are key differences between the two that affect how they are used in your code.

Structs

Structs in Swift are value types. When you create an instance of a struct and assign it to a variable or pass it to a function, it is copied rather than referenced. This means that changes to one instance do not affect others.

Structs are particularly useful for representing simple data structures that do not require inheritance or reference semantics.

Defining a Struct

You define a struct using the struct keyword, followed by the struct name and its properties and methods.

Example:
struct Person {
    var name: String
    var age: Int

    func introduce() {
        print("Hi, my name is \(name) and I am \(age) years old.")
    }
}

Using a Struct

Here’s how you can create an instance of the struct and use its method:

Example:
let person1 = Person(name: "Alice", age: 30)
person1.introduce()
Output:
Hi, my name is Alice and I am 30 years old.

Classes

Classes in Swift are reference types. When you create an instance of a class and assign it to a variable or pass it to a function, you are working with a reference to that instance, not a copy. This means that changes to one reference will affect all references to that instance.

Classes are useful when you need to model more complex behaviors, particularly when you want to utilize inheritance.

Defining a Class

You define a class using the class keyword, followed by the class name and its properties and methods.

Example:
class Vehicle {
    var type: String
    var wheels: Int
    
    init(type: String, wheels: Int) {
        self.type = type
        self.wheels = wheels
    }

    func description() {
        print("This is a \(type) with \(wheels) wheels.")
    }
}

Using a Class

Here's how you can create an instance of the class and use its method:

Example:
let car = Vehicle(type: "Car", wheels: 4)
car.description()
Output:
This is a Car with 4 wheels.

Key Differences Between Structs and Classes

  • Type: Structs are value types, whereas classes are reference types.
  • Inheritance: Classes can inherit from other classes, while structs cannot.
  • Mutability: Structs are immutable by default unless marked with var, while class instances can be modified regardless.
  • Performance: Structs can be more efficient for small data types due to their value type nature.

Conclusion

Understanding structs and classes is essential for effective programming in Swift. Choose structs for simpler, immutable data structures and classes for more complex, reference-based models. By mastering these concepts, you'll be well on your way to writing clear and efficient Swift code.