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

How can I call the tableView on viewDidLoad?

I'm new to iOS and I'm trying to output the data from the selectedContact. It works with the addressLabel.text but I don't know how to call it when it's a table view? class ContactDetailsVC: UITableViewController { @IBOutlet var…
2
votes
1 answer

Set a bool value using model object didSet

I'm struggling to figure out how to properly set bool values using a model object's didSet. My app has a series of swipable cards where some flip and some don't. This code below is the CardView which is run for each card created. Currently, the code…
ericathanas
  • 183
  • 1
  • 6
2
votes
1 answer

How to wait for variable in Swift before executing function? (Swift)

below is my code. I want the value for Latitude and Longitude in my Poststring. But When he do the poststring my values are still nil because swift didn't update the location yet. So how can I wait for latitude and longitude before poststring gets…
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
2
votes
1 answer

didSet not working on variable

I'm trying to pass data from my cellClass to a viewController. I can easily pass the label text from cellClass to vc but can't set the variable in vc. This is my model class: class Item: NSObject { var itemId: String? var itemLabel:…
Mohit
  • 45
  • 2
  • 10
2
votes
1 answer

When/How - Outlet -> didSet

I'm wondering, when/how does the didSet on an outlet actually trigger? For example : @IBOutlet weak var modifyButton: UIButton! { didSet { modifyButton.layer.cornerRadius = 9 } }
CeraMo69
  • 33
  • 1
  • 10
2
votes
3 answers

How can I set a string variable and make it always lowercase?

I want to set a string variable and want to keep it always lowercase. This is my code : var alwaysLowercaseString : String? { didSet{ alwaysLowercaseString = alwaysLowerCaseString!.lowercaseString } } But when I use it, it goes…
ZeroChow
  • 422
  • 2
  • 5
  • 16
2
votes
0 answers

Computed property must have an explicit type when using didSet and long hand syntax ()

This is good: var strings:Array = [] { didSet { println("did it") } } This gives a build error Computed property must have an explicit type: var strings = Array() { didSet { println("did it") …
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
2
votes
0 answers

struct and class didSet procedures behaving differently

I've filed this as a radar report, but I'm not sure what they will do. What I have, is a struct, and that struct is instantiated and aggregated by a class instance. Since a struct is an object, it has setters and getters, as well as set/get…
Chris Marshall
  • 4,910
  • 8
  • 47
  • 72
1
vote
2 answers

Stop firing didSet to avoid infinite recursion with didSet in Swift with multiple properties

I have a class with two properties whose value is set using didSet. class MyClass { var myProp1: Bool { didSet { self.myProp2 += blinks ? 1 : -1 } } var myProp2: Int { didSet { self.myProp1…
ArturJD96
  • 13
  • 2
1
vote
2 answers

SwiftUI: Action not triggering with toggle

I'm trying to trigger an action with a toggle switch. In this case the print message "value did change" doesn't show up on console after clicking on the toggle. This is targeted for a macOS 10.15 app, .onChange will not work. struct ContentView:…
Eric
  • 673
  • 1
  • 7
  • 23
1
vote
1 answer

swift - didSet not called on UIImageView

How can I get didSet to recognize when imageView.image has been set? It only seems to be called during initialization. @IBOutlet weak var imageView: UIImageView! { didSet { //do stuff } }
squarehippo10
  • 1,855
  • 1
  • 15
  • 45
1
vote
1 answer

keep original slider value when slider become used for another task

I want my swift code below to keep the slider value of the width constraint when the sliders value is used for something else. You can see what is going on the gif below. When the value of the slider is used for something else the constraint…
Sam Burns
  • 65
  • 1
  • 10
1
vote
1 answer

why cant i change image on button click?

I am trying to change an image on button click i am using didset to do so where when a variable changes so does the image butt it isn't working even though the variables are changing, here is my code: the variable too change: var page:Int=1 { …
Camille Basbous
  • 299
  • 5
  • 11
  • 34
1
vote
0 answers

didSet Not Observing Static Variables

I'm trying to use didSet on a static observed property but it's not running the if-else statement in the didSet. Here is the code: static var isCorrect: Bool = false { didSet { if isCorrect { …
nerdintown
  • 115
  • 1
  • 7