Swift: Structures

Structures in Swift:

Let’s begin with the Structures in Swift.

Structure Creation in Swift:

Structures, also called structs, can use to represent real-life objects in code. The struct keyword is used to create structures, which are subsequently named and provided a body containing its properties and methods.

struct Student {
  var name: String
  var age: Int
 
  init(name: String, age: Int, gender: String) {
    self.name = name
    self.age = age
  }
}

Default Property Values:

To avoid assigning values during initialization, properties in a structure might have preassigned default values. These properties values can assign a value during initialization if desired.

struct Bus {
  var seatCapacity = 32
  var lenghtInFeets = 25
}
 
var schoolBus = Bus(seatCapacity: 36)
 
print(schoolBus.seatCapacity) // Prints: 36
print(schoolBus.lenghtInFeets)    // Prints: 25

Structure Instance Creation:

The name of the structure is used with parentheses () and any necessary arguments to create a new instance of the structure.

struct Employee {
  var name: String
  var age: Int
  var gender: String
  
  init(name: String, age: Int, gender: String) {
    self.name = name
    self.age = age
    self.gender = gender
  }
}
 
// Instance of Person:
var jake = Employee(name: "jake", age: 26, gender: "male")

Checking Type:

The type(of:) is a built-in function that takes an argument and returns the type of the argument.

print(type(of: "swift")) // Prints: String
print(type(of: 100))     // Prints: Int
print(type(of: false))   // Prints: Bool

init() Method:

An init() method on a structure can use to set values for an instance’s properties. The func keyword is not necessary for the init() method, unlike other methods. Hence the body can use the self keyword to refer to the structure’s actual instance.

struct Bus {
  var seatCapacity: Int
  var lenghtInFeet: Int
  
  init(seatCapacity: Int, lenghtInFeet: Int) {
    self.seatCapacity = seatCapacity
    self.lenghtInFeet = lenghtInFeet
  }
}
 
var schoolBus = Bus(seatCapacity: 32, lenghtInFeet: 24)

Structure Methods in Swift:

Methods are similar to functions in that they use a specific instance as a parameter. To call the method, use dot notation to append an instance to the method name, followed by parentheses holding any required arguments.

struct Cat {
  func meowing() {
    print("Meow")
  }
}
 
let casper = Cat()
casper.meowing() // Prints: Meow

Mutating Methods:

Structure methods use the mutating keyword to allow them to affect an instance’s own properties.

struct Grocery {
  var itemsInList = ["Eggs", "Tissues"]
 
  mutating func addToList(item: String) {
    self.itemsInList.append(item)
  }
}
 
var groceryList = Grocery()
 
groceryList.addToList(item: "Bread")
print(groceryList.itemsInList) 
// Prints: ["Eggs", "Tissues", "Bread"]

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: