Struct in Swift

Struct in Swift

In Swift, struct is used to store variables of different data types. It is a way to create a custom data type that groups related values together.


4 min read

In Swift, a struct is used to store variables of different data types. It is a way to create a custom data type that groups related values together. It's like making a blueprint for a particular kind of data. Structs are useful because they ensure that the data remains safe and doesn't change unexpectedly. They have properties (variables or constants) to store data and can also have functions to perform actions related to that data.

Think of a struct like a blueprint for storing information about something. For example, if you want to keep track of a person's name and age, you could create a struct that has slots for "name" and "age." Once you have this blueprint, you can use it to store information about many different people. It's like making a template for organizing data, so you don't have to do it from scratch for each person.

Structs are value types, which means they are copied when assigned to a new variable or passed as a function argument. They are one of the fundamental building blocks of the language and are widely used for modeling data in a clean and efficient way.

Why Use a Struct?

Structs offer a bunch of benefits, making your life as a programmer easier:

  1. Data Organization: Structs help you organize related data. For instance, if you want to store information about a person, you can create a struct to hold their name, age, and any other details you need.

  2. Simplicity: Structs are straightforward to use. You define the structure, and Swift takes care of the rest. No complex jargon or confusion.

  3. Value Type: Structs are value types, which means when you make a copy of a struct, you're essentially creating a brand-new one. This makes your code safer and prevents unintended changes to data.

Features of structs in Swift

  1. Properties: Structs can have properties, which are variables or constants that store data. These properties can be of various data types, including other structs, enums, and more.

  2. Methods: You can define methods within a struct to provide functionality specific to the data it represents. Struct methods can be both mutating (modifying the struct's properties) and non-mutating (read-only).

  3. Initialization: Structs can have custom initializers to create instances with specific initial values. Swift provides an automatic memberwise initializer that initializes all properties.

  4. Extensions: You can extend existing structs to add new properties, methods, and initializers, even if you don't have access to the original source code.

Define Swift Structure

Creating a struct in Swift is a breeze. Here's how it's done:

struct Person {
    var name: String
    var age: Int

In this example, we've defined a struct named "Person" with two properties: "name" (a string) and "age" (an integer). Here,

  • struct - keyword used to define a structure

  • StructName - the name of the structure

  • The variables and constants inside a struct are called properties.

Using Structs

Once you've defined your struct, you can create instances of it to store specific data:

let john = Person(name: "John", age: 30)
let alice = Person(name: "Alice", age: 25)

Now you've got two instances of the "Person" struct, one for John and one for Alice, each holding their unique information.

Accessing Struct Data

You can access the data within a struct using dot notation. For instance, if you want to get John's age:

let johnsAge = john.age

It's that simple! You use the instance name, a dot, and the property name.


Structs can be changed if you declare them with "var" instead of "let." For example:

var john = Person(name: "John", age: 30)
john.age = 31


Structs in Swift provide a convenient way to store and work with related data. They're easy to define, understand, and use, making them a perfect choice for many tasks. Whether you're managing data about people, products, or any other entities, struct is a handy tool in your Swift toolbox. Happy coding!

More Swift Tutorials

Follow me for more such content.

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!