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
0
votes
1 answer

'didSet' certain elements inside Arrays - Swift

I have an array, with multiple values. I want to detect if one of those values is changed, something like this: var array = [ 1, 2, 3, 4 { didSet{ print("Value Changed")}}, 5, 6 ] Is that…
0
votes
2 answers

How to change backgroundColor of UIView with didSet on a property?

I have a UIView with different states defined with an enum. When I change the state, I would like to update its backgroundColor propertys. It doesn't work. enum State { case lock case unlock case done } …
cmii
  • 3,556
  • 8
  • 38
  • 69
0
votes
1 answer

Swift: Variable values don't carry over to didSet

This is my first project in Swift so please bear with me. punkteLimit should be initialized with value 30. The value of the variable as well as a label should be updated everytime a slider value is changed. var punkteLimit: Int = 30 @IBAction func…
Fussinger
  • 433
  • 2
  • 11
0
votes
4 answers

Swift didSet does not fire

Suppose I have class ExerciseSet with 3 properties: id, name, isEnabled. I have an array of objects of this class: var exerciseSets: [ExerciseSet] = [] { didSet { ExerciseSet.syncWithPList(updatedSets: exerciseSets) } } Somewhere in…
realvadim
  • 154
  • 12
0
votes
0 answers

Swift variable with both 'get' and 'didSet'

I'm trying to have a swift 3 Date variable that takes action when it's set. Part of that action is to call a function in the class to handle side effects. The problem I'm running into is that method is also called from elsewhere, and part of what…
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
0
votes
0 answers

Property observer not getting hit, but value is changing

I have got a variable which seems to be changing throughout the app. I have confirmed this by when I originally change a value to be true and then print, it shows that it is then false, false, false, true, false, false, false and my property…
Jake Walker
  • 305
  • 2
  • 9
0
votes
1 answer

didSet usage recommendation?

Assume I have a video player with resize button for full/small screen. Writing resizing (frames, autolayout constants, and others) codes in a variable called isVideoFullScreen variable's didSet and just calling for IBAction button ->…
turushan
  • 690
  • 7
  • 25
0
votes
2 answers

assigning willSet block to iOS isResting property

I would like to set a willSet statement to isResting property. Whenever isResting is changed some function is called. This is what I mean: starNode.physicsBody?.isResting: Bool = false{ willSet{ if newValue == true{ …
potato
  • 4,479
  • 7
  • 42
  • 99
-1
votes
3 answers

Why does setting property in didSet of published property cause infinite loop?

I'm attempting to use SwiftUI Toggle to control the state of a boolean stored on the server. On the didSet of the @Published property, I'm making a network call to persist the state. Successful network calls work great. On failure, I am attempting…
-1
votes
1 answer

swift property observer for data processing

How can I get notified when data has changed to process it? I have a simple code for easy to understand what I want: class Data { var data: [Int] = [] { didSet { print("changed") } } } class Process { var…
Anton
  • 339
  • 5
  • 15
-1
votes
1 answer

What is the difference between when binding data to tableview cell iOS by using didSet method or using a function in iOS, Swift

we can bind data to the UITableViewCell like below in didSet method. class NameCell: UITableViewCell { @IBOutlet weak var nameLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() } override func…
Marlon Brando aka Ben
  • 863
  • 1
  • 14
  • 33
-1
votes
2 answers

Variable - didSet and willSet

I understand how to use didSet and willSet for variables but not really why. When should I use these methods and why? Usually when you update a value you have some kind of button for example where you update a textField and then set a variables…
user8200258
-1
votes
2 answers

Cannot call a function within a didSet. Error message: "expression resolves to unused function"

class HomeViewController: UIViewController { // The timer is used to check when the download is complete and will only allow segues when it is var timer = Timer() var segueName: String = "" static var didSelectTabBar = false static var…
-1
votes
3 answers

swift ios why use didSet in this example

I am trying to understand why didSet it used in this example. Its a piece from a pagecontroller that displays images (image slider) Code: import UIKit class PageItemController: UIViewController { // MARK: - Variables var itemIndex: Int =…
user2636197
  • 3,982
  • 9
  • 48
  • 69
-1
votes
1 answer

Swift didSet throwing error

I got the following code: var languageString: String = "" { didSet { if(languageString != oldValue) { configureView() } if let popoverController = languagePopoverController { …
Pedro.Alonso
  • 1,007
  • 3
  • 20
  • 41