structure

Structural methods

struct Dog {
  func bark() {
    print("Woof")
  }
}
let fido = Dog()
fido.bark() // prints: Woof

Mutation method (mutating)

struct Menu {
  var menuItems = ["Fries", "Burgers"]
  mutating func addToMenu(dish: String) {
    self.menuItems.append(dish)
  }
}

Using the Menu class

var dinerMenu = Menu()
dinerMenu.addToMenu(dish: "Toast")
print(dinerMenu.menuItems)
// prints: ["Fries", "Burgers", "Toast"]

Check type

print(type(of: "abc")) // print: String
print(type(of: 123))   // print: 123

init() method

struct TV {
  var size: Int
  var type: String

  init(size: Int, type: String) {
    self.size = size
    self.type = type
  }
}

Using the TV class

var newTV = TV(size: 65, type: "LED")

Structural instance creation

struct Person {
  var name: String
  var age: Int
  init(name: String, age: Int) {
    self.name = name
    self. age = age
  }
}
// Person instance:
var morty = Person(name: "Peter", age: 14)

Default property values

struct Car {
  var numOfWheels = 4
  var topSpeed = 80
}
var reliantRobin = Car(numOfWheels: 3)
print(reliantRobin.numOfWheels) // prints: 3
print(reliantRobin.topSpeed)    // print: 80

Structure Creation

struct Building {
  var address: String
  var floors: Int
  init(address: String, floors: Int) {
    self.address = address
    self. floors = floors
  }
}

Structs or structs are used to programmatically represent real-life objects in code. A structure is created using the struct keyword, followed by its name, followed by a body containing its properties and methods

Comments