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
1
vote
1 answer

Run code when date in DatePicker is changed in SwiftUI didSet function?

struct ViewName: View { @State private var selectedDay: Date = Date() { didSet { print("Old value was \(oldValue) and new date is \(self.selectedDay)") } } var body: some View { VStack { …
nickcoding
  • 305
  • 8
  • 35
1
vote
1 answer

.toggle() function on bool does not call didSet. Is this a bug?

I have a @State Bool variable with didSet on it. I want to do something when the variable change therefore I have tried to use didSet. The problem is when I use the .toggle() function to toggle the state of the bool, didSet is not called. Take this…
Priva28
  • 327
  • 1
  • 10
1
vote
4 answers

didSet not called by Array.append() in Swift

I am following the 100 Days of SwiftUI and have reached Day 37. While doing Making changes permanent with UserDefaults, I encounter a problem with didSet. (I am using Swift 5 with iOS 13.4) In the example code, it…
PinkiePie-Z
  • 525
  • 1
  • 6
  • 28
1
vote
1 answer

didSet not getting called on every mutation?

I have a variable that I want to have a listener for when you set and mutate and didSet seems like the perfect candidate. However, in my initial testing of it, the first append triggers the did set and (in my example) the JS loads into my webview,…
user3338275
  • 240
  • 2
  • 3
  • 13
1
vote
2 answers

How to update .setTitle for a UIButton using didSet?

I'm working with a profile registration view controller, I want to update the title of my UIButton when the user picks the city from the UIPickerController. The title from the button is not updating after the user picks the city. I try using…
Robby
  • 197
  • 1
  • 2
  • 14
1
vote
0 answers

Swift property observers: why is didSet not called on an array property if the array is amended by appending a new array?

I have a string array and I also wish to create a localised copy of this array (whilst keeping the original array intact). There are a number of ways to do this, but I thought I might use a property observer to set the contents of the localised…
1
vote
1 answer

Swift willSet/didSet assignment?

I want to observe changes on couple of properties in my class by using the didSet/willSet API. However I'd like to keep my properties decelaration section clean, so I want to have separate functions for implementations of this logic. Right now I…
user3581248
  • 964
  • 10
  • 22
1
vote
3 answers

Why use didSet when you can just mutate a variable and the value changes anyway?

I read through this SO didSet q&a and Apple’s Property Observers and a few other posts. What I can’t seem to wrap my head around is what is the benefit of using didSet when mutating a variable when if you change the variable without using a property…
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
1
vote
1 answer

Swift variable observers not called before super.init called

Okay so I was reading up on how willSet/didSet are used in swift and I came across a note on apples swift docs that just doesn't make any sense to me and I hope someone can explain. Here's the note: The willSet and didSet observers of superclass…
user614273
  • 74
  • 10
1
vote
1 answer

How to use preference from other didSet variable in custom Cell?

I have a custom cell for a collectionView and inside I have 3 didSet variables. My problem is that I need to use 1 preference from each didSet to display on a label but I can't figure out a way to do so. Any suggestions? class VideosCell:…
user8896788
1
vote
1 answer

Did set isn't observing model property's change - Swift

Hey I'm using a didSet to set an Array of keys from a dictionary so that I can avoid replication when I try to access the array to populate collection view cells. The didSet is supposed to occur when a Cafe object gets changed but when it…
Sam
  • 495
  • 3
  • 11
  • 19
1
vote
1 answer

Swift 3 - didSet for the opposite direction?

Let's say I have these two classes : class A {} class B{ var myA:A? } I want to know when that happens: let b = B() let a = A() b.myA = a // <<<---- I want to observe this from my A class I would also like to know who is the owner(In this…
RawKnee
  • 324
  • 3
  • 11
1
vote
0 answers

Use of didSet property observer in custom UITableCell to update TextView

I have a table that displays a list of pupils and their chosen subjects. The pupil's name are displayed as table section headers and each chosen subject is displayed as table rows for each section. I have a simple class to store the pupil's name as…
Tom
  • 790
  • 2
  • 9
  • 32
1
vote
2 answers

Property Observer for Swift with Objects

I'm trying to add a property observer in my class ChooserListVC for "list" These are the variables in ChooserSaves that I would like to track. class ChooserSaves: UIDocument { var savedListObject : SavedList? var listName : String = "" var…
Stan
  • 31
  • 5
1
vote
2 answers

Setting label with property observer in UITableViewCell, what's happening

In my custom UITableViewCell when the property is set, if there is no label it will create a label and set the text etc. If the label already exists, it shouldn't do anything. The strange thing is, when I tap the cell for the first time it creates…
Henny Lee
  • 2,970
  • 3
  • 20
  • 37