Creating Structs

1
2
3
4
5
struct Fruit {
    var name: String
}

var banana: Fruit = Fruit(name: "banana")

Computed Properties

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
struct Fruit {
    var name: String
    var information: String {
        return "I am a \(name)"
    }
}

var banana: Fruit = Fruit(name: "banana")
print(banana.information)
// I am a banana

Property Observers

We can use didSet to observe when a property is modified

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
struct Fruit {
    var name: String {
        didSet {
            print("My name is now \(name)")
        }
    }
}

var banana: Fruit = Fruit(name: "banana")
banana.name = "nanaba"
// My name is now nanaba

Methods

Methods are just functions inside Structs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
struct Car {
    var speed: Int
    func calculateTime(distance: Int) -> Double {
        return Double(distance) / Double(speed)
    } 
}

var myCar: Car = Car(speed: 50)
print(myCar.calculateTime(distance: 200)
// 4.0

Mutating Methods

By default, methods can not mutate struct’s properties. When we want to change a property inside a method, wee need to use the mutating keyword

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
struct Car {
    var speed: int
    mutating func changeSpeed(to newSpeed: Int) {
        speed = newSpeed
    }
}

var myCar: Car = Car(speed: 50)
myCar.changeSpeed(to: 100)
print(myCar.speed)
// 100

Initializers

By default, new struct instances will be initialized automatically when created. We can specify how our instances will be initialized by using the init method.

init method does not have the func keyword in front of it

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
struct Student {
    var age: Int
    var bornIn: Int
    init(bornIn: Int) {
        self. bornIn = bornIn
        self.age = 2019 - bornIn
        print("Created Student who is \(self.age) years old")
    }
}

var linux:  Student = Student(bornIn: 1991)
// Created Student who is 28 years old

Notice that you have to set values to all of your properties.

Lazy Properties

Imagine that one of your properties is very time and memory consuming. You can use the lazy keyword to tell Swift to only create that property when it is called

1
2
3
4
5
6
struct Student {
    lazy var familyInfo: String = "long string..." // A very long string, maybe 5Gb in memory
}

var me: Student = Student()
me.familyInfo

Static Properties and Methods

Static Properties are Properties that belong to the Struct, not only the instances.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
struct ClassMember {
    static var studentNumbers = 0
    init() {
        ClassMember.studentNumbers += 1
    }
}

var bill: ClassMember = ClassMember()
var steve: ClassMember = ClassMember()
var linus: ClassMember = ClassMember()
print(ClassMember.studentNumbers)
// 3

Access Control

By default, struct’s properties are public, which means they can be modified anywhere. To make properties only accessible inside struct’s methods, we can add the private keyword.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
struct Sport {
    private var name: String
    func sayHi() {
        print("Hello I am \(name)")
    }
    init(name: String) {
        self.name = name
    }
}

var badminton: Sport = Sport(name: "badminton")
badminton.sayHi()