Structure
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
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
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)
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
Check type
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"]