Structs

#set #get #willSet #didSet #newValue #oldValue #method #property #StoredProperty #ComputedProperty #staticproperty #staticmethod

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

Before
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

*/
After
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

*/

...

If any change in blurAmount run didSet

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

Example for willSet

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,

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

self vs Self

self: The current value of struct

  • values: 56, "Siirt", true

Self: The current type of struct

  • types: Int, String, Bool

Sources

Sources

Videos

Articles / Documents

See Also

Last updated