Questions tagged [didset]

in Swift this defines a property observer that executes code after a new value has been set

See Property Observers in the The Swift Programming Language:

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.

You can add property observers to any stored properties you define, apart from lazy stored properties. You can also add property observers to any inherited property (whether stored or computed) by overriding the property within a subclass. Property overriding is described in Overriding.

107 questions
309
votes
11 answers

What is the purpose of willSet and didSet in Swift?

Swift has a property declaration syntax very similar to C#'s: var foo: Int { get { return getFoo() } set { setFoo(newValue) } } However, it also has willSet and didSet actions. These are called before and after the setter is called,…
zneak
  • 134,922
  • 42
  • 253
  • 328
270
votes
9 answers

Is it possible to allow didSet to be called during initialization in Swift?

Question Apple's docs specify that: willSet and didSet observers are not called when a property is first initialized. They are only called when the property’s value is set outside of an initialization context. Is it possible to force these to be…
Logan
  • 52,262
  • 20
  • 99
  • 128
64
votes
1 answer

Is there a didSet/willSet analog in Kotlin?

I love this Swift syntax; it's very helpful for many things: var foo: Bar = Bar() { willSet { baz.prepareToDoTheThing() } didSet { baz.doTheThing() } } and I'd love to do this in Kotlin. However, I can't find the…
Ky -
  • 30,724
  • 51
  • 192
  • 308
51
votes
5 answers

Property observers willSet and didSet; Property getters and setters

What is the difference between willSet - didSet, and get - set, when working with this inside a property? From my point of view, both of them can set a value for a property. When, and why, should I use willSet - didSet, and when get - set? I know…
just ME
  • 1,817
  • 6
  • 32
  • 53
28
votes
3 answers

Using didSet and private(set) on Swift Array

I'm working on a swift project and I have a couple of arrays. In one of my arrays, I do not want the client to be able to mutate it without using one of my specially-defined methods. On the other hand, I want the getter to be accessible. My…
raoul
  • 463
  • 1
  • 6
  • 10
28
votes
3 answers

Override property observer

When I override the function noise, the function gets replaced by the new one. But when I override a property with an observer, the old and new value gets both executed. In playground: class Vehicle { func noise(sound: String) { …
Eendje
  • 8,815
  • 1
  • 29
  • 31
27
votes
5 answers

What if I want to assign a property to itself?

If I attempt to run the following code: photographer = photographer I get the error: Assigning a property to itself. I want to assign the property to itself to force the photographer didSet block to run. Here's a real-life example: In the "16.…
Senseful
  • 86,719
  • 67
  • 308
  • 465
26
votes
4 answers

Swift: how to change a property's value without calling its didSet function

How can you set a property's value in Swift, without calling its didSet() function outside of an initialization context? The code below was a failed experiment to achieve this within the classes' noside() function class Test { var toggle :…
ctpenrose
  • 1,467
  • 2
  • 18
  • 28
24
votes
2 answers

How to implement the "didset of swift" in objective-c?

Swift (from the book 《iOS Animations by Tutorials:Chapter 12》 released by http://www.raywenderlich.com/): let photoLayer = CALayer() @IBInspectable var image: UIImage! { didSet { photoLayer.contents = image.CGImage } } How can I…
codingiran
  • 413
  • 1
  • 4
  • 8
21
votes
3 answers

In Swift, does resetting the property inside didSet trigger another didSet?

I'm testing this and it appears that if you change the value within didSet, you do not get another call to didSet. var x: Int = 0 { didSet { if x == 9 { x = 10 } } } Can I rely on this? Is it documented somewhere? I don't see it in…
Rob N
  • 15,024
  • 17
  • 92
  • 165
15
votes
3 answers

SwiftUI View Property willSet & didSet property observers not working

I have a SwiftUI (Beta 5) view with an attached ViewModel. I want to navigate to it via a navigationLink and pass in a simple parameter (called FSAC in this case) I navigate using NavigationLink("Next", destination: MyTestView(FSAC:…
Andy Thomas
  • 1,367
  • 2
  • 14
  • 30
14
votes
3 answers

Swift: Overriding didSet results in a recursion

When overriding the didSet observer of a property results in recursion, why? class TwiceInt { var value:Int = 0 { didSet { value *= 2 } } } class QuadInt : TwiceInt { override var value:Int { didSet…
chunkyguy
  • 3,509
  • 1
  • 29
  • 34
11
votes
4 answers

How to call didSet without changing property value

I need the code in didSet to be executed without changing the property's value. Coming from Objective-C recently, there doesn't seem to be a setMyProperty(). So I tried: self.myProperty = self.myProperty which result in error Assigning a property…
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
8
votes
1 answer

Is there a way to get didSet to work when changing a property in a class?

I have a class as property with a property observer. If I change something in that class, is there a way to trigger didSet as shown in the example: class Foo { var items = [1,2,3,4,5] var number: Int = 0 { didSet { items…
Henny Lee
  • 2,970
  • 3
  • 20
  • 37
6
votes
0 answers

SwiftUI @Binding in subview how I can observe changes (aka didSet) from parent

I would like to observe changes to @Binding property in subview. But there didSet/willSet is not called (it is called only if I change this variable from current view, but if change is from outside view then this handlers are not executed) I want…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
1
2 3 4 5 6 7 8