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

Why is this didSet property not printing any output in Swift?

I have correctly set up a UIPageViewController. I would like to check the itemIndex of each views and display content accordingly. Here's what the code looks like: var itemIndex: Int = 0 { didSet { if itemIndex == 1 { …
Cesare
  • 9,139
  • 16
  • 78
  • 130
0
votes
1 answer

Swift can lazy, get and didSet together

Can we do get and didSet together with lazy keyword. I have an array it fetch data from database (get) and another thing when that array will modify i will call delegate to reload data in didSet. Its not working and not complain of any error. …
0
votes
1 answer

Stuck at reassigning TableView Cell property

I'm working on an App where you can track your reading progress for Books. The HomeViewController contains a TableView that lists the books you have added. It has a progress Bar and shows what page you're on. The AddBookController is for adding Data…
alias.marv
  • 35
  • 2
0
votes
2 answers

Blinking symbol with didSet in SwiftUI

This is synthesized from a much larger app. I'm trying to blink an SF symbol in SwiftUI by activating a timer in a property's didSet. A print statement inside timer prints the expected value but the view doesn't update. I'm using structs throughout…
Phantom59
  • 947
  • 1
  • 8
  • 19
0
votes
1 answer

How to save a Stepper data to UserDefaults in SwiftUI?

struct Test { @State private var steps = UserDefaults.standard.integer(forKey: "steps") { didSet { steps = UserDefaults.standard.integer(forKey: "steps") } } var…
Aashish
  • 2,532
  • 2
  • 23
  • 28
0
votes
0 answers

Swift: array is set but is then empty when accessed

I have an array which is set once a specific API is called. However when I then try to access this array at a later point it is empty again. The class in question looks like this: class SearchableLoginFormField: LoginFormField { weak var…
DevB1
  • 1,235
  • 3
  • 17
  • 43
0
votes
2 answers

What is the proper way to update my label's text from a function?

I am trying to have my UILabel's text automatically update in increments of 1 based on a looping Timer. The label is linked to a variable. I'm not sure if it matters but I am doing my UI programmatically using auto layout anchors. I understand this…
pawCheese
  • 13
  • 1
0
votes
2 answers

UITextField in Swift – text didSet not called when typing

I am writing a custom UITextField right now in Swift, and encountered the following: class MyField: UITextField { open override var text: String? { didSet { // some logic that normalizes the text } } …
BlackWolf
  • 5,239
  • 5
  • 33
  • 60
0
votes
2 answers

Property Observer in Swift

Any idea how I can return a UIView from within didSet ? I have a method that returns a UIView. I need to observe an Int and return a UIView as the Int changes. I have a didSet observer set, however, I get an error when trying to return the…
0
votes
1 answer

Variable listener in iOS Swift

I am trying to make an independent class/library that runs on its own and handles errors internally, but also gives feedback (status) to the frontend for possible user interaction. In Android I'd solve this with a Listener: class…
André
  • 142
  • 1
  • 15
0
votes
0 answers

didSet thread error on data retrieval, why is called function not fully executing?

I am testing a situation that some users might face where they might open the app without an internet connection, and at some point, while using the app the connection reappears and thus I would like for them to retrieve their data. The data is…
Camille Basbous
  • 299
  • 5
  • 11
  • 34
0
votes
2 answers

didSet not updating NSImage

I am trying to update the Image of an NSButton(Image) through a toggle in my storyboard. The NSButton Image is not changing though. Here my ViewController IBAction Code: @IBAction func ButtonState(_ sender: NSSwitch) { let stateOfButton =…
Scholz
  • 3
  • 3
0
votes
1 answer

A property in a struct not being updated after being computed in willSet observer

Sorry in advance if my question comes across being stupid, I'm currently learning property observers and I've been given an example from a great swift tutorial online to determine if the code is valid, I correctly assumed it was and decided to…
Yuni
  • 25
  • 7
0
votes
1 answer

didSet not updating UILabel.text with async data

I am trying to show the username of a logged-in user when logging in using googleSignIn. The googleSignIn has been set up in appdelegate and the username to display is in the viewController that launches the sign in. For a strange reason, I can…
0
votes
1 answer

color of cgpoint changes all lines and should only change new lines

My code uses to classes. When the function dizzy is called it changes the color of all the lines in the uiview. What I want it to do is only change lines colors that are drawn after the function is called. It should not change the color of the lines…
user12274342