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
6
votes
2 answers

Swift - weak self in didSet

I rarely see people using [weak self] in didSet. Is there a reason for this? I tried to use [weak self] in my didSet of a variable: var data: Dictionary! { // [1] didSet { [2] self?.layoutSubviews() } …
quanguyen
  • 1,443
  • 3
  • 17
  • 29
5
votes
1 answer

SwiftUI - usage of toggles - console logs: “invalid mode 'kCFRunLoopCommonModes'” - didSet does not work

I have a general problem using toggles with SwiftUI. Whenever I use them I get this console error: invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message…
Kuhlemann
  • 3,066
  • 3
  • 14
  • 41
5
votes
2 answers

Loops in didSet

We came across this odd behaviour when using loops in a didSet. The idea was that we have a data type with a tree structure and in each element we wanted to store the level that item is on. So in the didSet of the level attribute we would also set…
M. Schrick
  • 61
  • 5
5
votes
1 answer

Are willset and didset considered closures in Swift?

I understand the purpose of willset and didset my I am not sure if they are considered closures. If they were closures, shouldn't the following code produce a strong reference cycle? var myProperty : Int = 0 { didSet { self.callMyMethod() } }
agy
  • 2,804
  • 2
  • 15
  • 22
5
votes
1 answer

Why Swift Array cannot be altered in property observer didSet?

It seems that Swift's Array won't go through didSet, why? var intArray: [Int] = [] { didSet { intArray += [0] } } if intArray.count == 0 { println("Why is intArray not being altered?") }
Bruce You
  • 167
  • 1
  • 9
4
votes
1 answer

Inner didSet protection bizarrely extends to the whole class?

It's well-known that, of course, didSet will not run on the same object again from inside a didSet. (example.) However. It seems that: the restriction applies not only to that object, but to maybe any object of the same class. Here are copy-paste…
Fattie
  • 27,874
  • 70
  • 431
  • 719
4
votes
1 answer

Why no Infinite loop in didSet?

In my FirstViewController I have a button directing to my SecondViewController, passing data to a property in the SecondViewController. This property has a property observer, creating a new instance of the SecondViewController when set. While it's…
Henny Lee
  • 2,970
  • 3
  • 20
  • 37
4
votes
3 answers

Implement variable attribute property observer in Swift

I want to implement a didSet over a "sub-attribute" of a variable. Example: @IBOutlet weak var myLabel: UILabel! var myLabel.hidden { didSet{ "DO SOMETHING" } } I want to hide/show some other views when myLabel.hidden attribute change. How can I do…
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
4
votes
1 answer

Dynamically setting up willSet & didSet in Swift

I see potential for use of willSet & didSet to replace parts of KVO-type code I'd use in Objective-C. One of the benefits of Objective-C is it's dynamism, specially the ability to create behavior at runtime. In order for willSet & didSet to be…
james_womack
  • 10,028
  • 6
  • 55
  • 74
3
votes
3 answers

Why can't I remove elements from an Array in its willSet event?

The logic is to clear an Array when it has a specified amount of elements. I could put the check outside of the Array but I was trying to see what if do it in Array's willSet event. The result is elements in Array stay still. Here is the code var…
Alan Hoo
  • 445
  • 3
  • 12
3
votes
1 answer

didSet in Javascript: calling function when variable is altered

Is there an equivalent to didSet from Swift in Javascript? Basicall to call a function automatically when a variable is altered. The purpose of this would be instead of having to call a function each time as follows: var x = someType() x.behavior =…
Anters Bear
  • 1,816
  • 1
  • 15
  • 41
3
votes
3 answers

Why does a property observer run when a member of the existing value is changed?

Please consider this Swift code. I have a class which wraps an instance of another class. When I set a property on the held value, the wrapper class's property observer is run. protocol MyProtocol { var msgStr: String? { get set } } class…
Akshay Shah
  • 1,120
  • 6
  • 13
3
votes
2 answers

Swift trigger didSet when init

Why this code will trigger didSet when init final public class TestDidSet { static var _shared: TestDidSet! = TestDidSet() func testA() { } private var test = true { didSet { print("didSet test when initing!!!!!:\(test)") …
InvokerLaw
  • 91
  • 9
2
votes
1 answer

SwiftUI onReceive is it possible to get oldValue?

I have a custom TabView and I want to Bind to a State to change tabs. I also want to detect if the user has tapped the same tab again in order to scroll to the top of that view. didSet isn't called when I use a binding. onChange isn't called because…
Dan
  • 543
  • 2
  • 13
2
votes
1 answer

SwiftUI: didSet doesn't update view

In the following, reproduced example, I have the following code which makes up a single screen. There's a lot of code, but it's simple! The intended function: The screen is filled with a plain color. Below the color, there is a picker…
chuk
  • 69
  • 5