0 votes
in ios by (290 points)

7 Answers

0 votes
by (610 points)
The point seems to be that sometimes, you need a property that has automatic storage and some behavior, for instance to notify other objects that the property just changed. When all you have is get/set, you need another field to hold the value. With willSet and didSet, you can take action when the value is modified without needing another field. For instance, in that example:

class Foo {

    var myProperty: Int = 0 {

        didSet {

            print("The value of myProperty changed from \(oldValue) to \(myProperty)")

        }

    }

}

myProperty prints its old and new value every time it is modified. With just getters and setters, I would need this instead:

class Foo {

    var myPropertyValue: Int = 0

    var myProperty: Int {

        get { return myPropertyValue }

        set {

            print("The value of myProperty changed from \(myPropertyValue) to \(newValue)")

            myPropertyValue = newValue

        }

    }

}

So willSet and didSet represent an economy of a couple of lines, and less noise in the field list.
0 votes
by (380 points)

Property observers observe and respond to changes in a property’s value. Property observers are called every time a property’s value is set, even if the new value is the same as the property’s current value . Let's have a look at the code snippet below to help us have a better understanding of how property observers willSet and didSet works.  

import UIKit

struct Person {

    var name: String

    var age: Int

}

class ViewController: UIViewController {

    var person: Person? {

        didSet{

            print("Called after setting the new value")

            if let name = person?.name {

                print("New name is \(name) and old name is \(String(describing: oldValue?.name))")

            }

        }

        willSet(myNewValue) {

            print("Called before setting the new value")

            if let newName = myNewValue?.name {

                print("New name is \(newName)")

            }

        }

    }

    override func viewDidLoad() {

        super.viewDidLoad()

        person = Person(name: "Shem", age: 4)

    }

}

Property observers are declared as a variable and not as constants because it is only a mutable property that can be tracked by property observers. Hence, property observers are declared with var and not the let keyword.

0 votes
by
How Much Amoxicillin Do I <a href=https://buycialikonline.com>Cialis</a>
0 votes
by
Both receptors are present in the lung, with ERОІ being more abundant than ERО± 12 <a href=http://stromectol.autos/>stromectol precio mexico</a>
0 votes
by
<a href=http://nolvadex.one/>nolvadex reduce puffy nipples</a> Although primarily the drug is an anti estrogen, it also possess strong testosterone stimulating characteristics
0 votes
by
primidone will decrease the level or effect of riluzole by affecting hepatic enzyme CYP1A2 metabolism <a href=http://stromectol.ink/>stromectol for sale online</a>
0 votes
by
hi opp ggeis 2022 ert go fi
Welcome to Reubro Q&A, where you can ask questions and receive answers from other members of the community.
...