Easy Tutorial
❮ Swift Generics Swift Tutorial ❯

Swift Structs

Swift structs are a versatile and flexible construct used to build code.

We can define properties (constants, variables) and add methods to structs to extend their functionality.

Unlike C and Objective-C:

Structs are always passed by copy in code, making their values immutable.

Syntax

We define a struct using the keyword struct:

struct nameStruct { 
   Definition 1
   Definition 2
   ……
   Definition N
}

Example

We define a struct named MarkStruct with properties for the marks of three subjects, all of type Int:

struct MarkStruct {
   var mark1: Int
   var mark2: Int
   var mark3: Int
}

We can access struct members using the struct name.

Struct instantiation uses the let keyword:

import Cocoa

struct studentMarks {
   var mark1 = 100
   var mark2 = 78
   var mark3 = 98
}
let marks = studentMarks()
print("Mark1 is \(marks.mark1)")
print("Mark2 is \(marks.mark2)")
print("Mark3 is \(marks.mark3)")

The output of the above program is:

Mark1 is 100
Mark2 is 78
Mark3 is 98

In the example, we access the student's marks using the struct name 'studentMarks'. The struct members are initialized to mark1, mark2, mark3, all of type Int.

We then instantiate the struct studentMarks() and assign it to marks using the let keyword.

Finally, we access the struct members' values using the dot . notation.

The following example demonstrates struct instantiation with values and cloning:

import Cocoa

struct MarksStruct {
   var mark: Int

   init(mark: Int) {
      self.mark = mark
   }
}
var aStruct = MarksStruct(mark: 98)
var bStruct = aStruct // aStruct and bStruct are structs with the same values!
bStruct.mark = 97
print(aStruct.mark) // 98
print(bStruct.mark) // 97

The output of the above program is:

98
97

Struct Applications

In your code, you can use structs to define custom data types.

Struct instances are always passed by value.

Consider building a struct when one or more of the following conditions apply:

Examples of suitable scenarios for structs include:

Struct instances are passed by value rather than by reference.

import Cocoa

struct markStruct {
    var mark1: Int
    var mark2: Int
    var mark3: Int

    init(mark1: Int, mark2: Int, mark3: Int) {
        self.mark1 = mark1
        self.mark2 = mark2
        self.mark3 = mark3
    }
}

print("Excellent Marks:")
var marks = markStruct(mark1: 98, mark2: 96, mark3: 100)
print(marks.mark1)
print(marks.mark2)
print(marks.mark3)

print("Poor Marks:")

The output of the above program is:

Excellent Marks:
98
96
100
Poor Marks:
var fail = markStruct(mark1: 34, mark2: 42, mark3: 13)
print(fail.mark1)
print(fail.mark2)
print(fail.mark3)

The output of the above program is:

Excellent scores:
98
96
100
Poor scores:
34
42
13

In the above example, we defined the structure markStruct with three member properties: mark1, mark2, and mark3. The structure uses the self keyword to access its member properties.

From this example, we can understand that instances of a structure are passed by value.

❮ Swift Generics Swift Tutorial ❯