Questions tagged [property-observer]

52 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
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
22
votes
2 answers

Why is didSet called twice on the TextField binding in SwiftUI?

I have a very basic view that only shows a TextField: View struct ContentView: View { @StateObject var viewModel = ViewModel() var body: some View { TextField("Enter a string...", text: $viewModel.string) } } The…
Mischa
  • 15,816
  • 8
  • 59
  • 117
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
11
votes
2 answers

Can I use didSet in deinit?

I added a variable of Timer to my class, and used its didSet observer to invalidate old value var timer: Timer? { didSet { oldValue?.invalidate() } } deinit { timer = nil } I thought this would be enough to invalidate timer when class is…
Damian Dudycz
  • 2,622
  • 19
  • 38
8
votes
1 answer

Does Java have property observers similar to Swift's willSet & didSet methods?

I want to observe whether a variable's value has been changed. I was wondering if Java has something that is equivalent to the willSet and didSet methods in Swift?
Thor
  • 9,638
  • 15
  • 62
  • 137
7
votes
4 answers

What makes a property a computed property in Swift

Let's started with the code snippet: St Foo { var proA: Int = 0 { // needs initialization willSet { print("about to set proA to \(newValue) from \(proA)") } didSet { print("already set proA to…
SLN
  • 4,772
  • 2
  • 38
  • 79
4
votes
2 answers

UIViewController isEditing - Property observer doesn't work

I was trying to implement property observers on my custom UIViewController but I noticed it was not working with the isEditing property. Do you guys have an idea why? class MasterViewController: UIViewController { // MARK: - Properties …
Berty86
  • 637
  • 6
  • 13
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
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
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
1 answer

Mixing Swift Property Observers and Inheritance

I've been tinkering with this, and I was wondering if there is any literature on this subject, or any way of knowing what is the expected behavior. Here is an example: class A { var paused:Bool = false { willSet { print("Class A willSet on…
mogelbuster
  • 1,066
  • 9
  • 19
2
votes
3 answers

How to call a method once two variables have been set

I am using iOS Swift, and I am trying to understand how to execute a method once the value of two variables have been set up (non-null value) once the requests have finished. After reading some documentation, I have found out some concepts which are…
bellotas
  • 2,349
  • 8
  • 29
  • 60
2
votes
1 answer

Defer statement at the end of deinit produces a warning

Since the Xcode 10.2 (Swift 5) the defer statement at the end of the deinit scope produces: 'defer' statement before end of scope always executes immediately; replace with 'do' statement to silence this warning Let's take a look at this…
Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
2
votes
3 answers

Struct not calling property observers on new object

In this code, I would like to have the property observers called when the new object is created. How do I achieve this? Here is what I have so far: struct MyStruct { var myValue: Int { willSet { print("willSet") } …
George
  • 25,988
  • 10
  • 79
  • 133
1
2 3 4