# Structs

## Overview

Struct 's are simpler and faster than classes.

For SwiftUI Views we should use Struct rather than Class.

**Stored Property vs Computed Property**

1. Stored Property (a data to be used later)
2. Computed Property (fresh calculation / depends on other properties)
   * ReCalculates the property everytime the struct accessed
   * They accessed like Stored Properties but act like Methods because they run some code based on properties

\* Variables and Constants in Structs are Properties

### Lazy Properties

### Methods

### set / get

### willSet / didSet

Property observers that take action, when about to change (willSet) and after the change (didSet).

It is quite useful when a property constantly needs to print during those change stages.

Do not forget that every time didSet and willSet trigger app takes action, so do not add so much work on them for sake of not having performance issues.

#### Example for didSet

{% code title="Before" %}

```swift
struct Game {
    var score = 0 // setting a default number
}

var game = Game()
game.score += 10
print("Score is now \(game.score)")
game.score -= 3
print("Score is now \(game.score)")
game.score += 1
print("Score is now \(game.score)")

/*
print:

Score is now 10
Score is now 7
Score is now 8

*/
```

{% endcode %}

{% code title="After" %}

```swift
struct Game {
    var score = 0 { // setting a default number
    
        didSet {
            print("Score is now \(score)")
        } // triggers a print action every time the value changes
    
    }
}

var game = Game()
game.score += 10
game.score -= 3
game.score += 1

/*
print:

Score is now 10
Score is now 7
Score is now 8

*/
```

{% endcode %}

...

If any change in blurAmount run didSet

```swift
@State private var blurAmount = 0.0 {
    didSet {
        print("New value is \(blurAmount)")
    }
}
```

#### Example for willSet

```swift
struct App {
    var contacts = [String]() {
        willSet {
            print("Current value is: \(contacts)")
            print("New value will be: \(newValue)")
        }

        didSet {
            print("There are now \(contacts.count) contacts.")
            print("Old value was \(oldValue)")
        }
    }
}

var app = App()
app.contacts.append("Adrian E")
app.contacts.append("Allen W")
app.contacts.append("Ish S")

/*
print:

Current value is: []
New value will be: ["Adrian E"]
There are now 1 contacts.
Old value was []

Current value is: ["Adrian E"]
New value will be: ["Adrian E", "Allen W"]
There are now 2 contacts.
Old value was ["Adrian E"]

Current value is: ["Adrian E", "Allen W"]
New value will be: ["Adrian E", "Allen W", "Ish S"]
There are now 3 contacts.
Old value was ["Adrian E", "Allen W"]

*/
```

**Sources:** [HWS](https://www.hackingwithswift.com/quick-start/beginners/how-to-take-action-when-a-property-changes),&#x20;

### newValue / oldValue

### static let & static var & static func

The main davantage of static over regular is you can **you use let & var & func across the app**, whereever you need them.

private static ??? #learn

#### Sources

* [Hacking with Swift / 100 days / 11 - Structs, part two](https://www.hackingwithswift.com/100/swiftui/11)

### self vs Self

**self**: The current **value** of struct

* **values:** 56, "Siirt", true

**Self**: The current **type** of struct

* **types:** Int, String, Bool

#### Sources

* [Hacking with Swift / 100 days / 11 - Structs, part two](https://www.hackingwithswift.com/100/swiftui/11)

## Sources

### Videos

### Articles / Documents

* [Hacking With Swift / 100 days / 10 - Structs Part-1](https://www.hackingwithswift.com/100/swiftui/10)

## See Also

* [class-vs-struct](https://swift.sedatonat.dev/codes/main-pillars-protocol-class-struct-etc./class-vs-struct "mention")
