Swift didSet get index of array
The zip() function could be useful for this:
class A
{
var array = [1,2,3,4,5]
{
didSet
{
let changedIndexes = zip(array, oldValue).map{$0 != $1}.enumerated().filter{$1}.map{$0.0}
print("Changed indexes: \(changedIndexes)")
}
}
}
let a = A()
a.array = [1,2,7,7,5]
// prints: Changed indexes: [2, 3]
It also works for single element changes but arrays are subject to multiple changes so its safer to get an array of changed indexes.
didSet' certain elements inside Arrays - Swift
Swift 3.0
You can do like below to Observer which index of array is changed.
var oldArray: [Int] = []
var array = [ 1,2,3,4,5,6] {
willSet {
// Set old array value for compare
oldArray = array
}
didSet {
let changedIndex = zip(array, oldArray).map{$0 != $1}.enumerated().filter{$1}.map{$0.0}
print("index: \(changedIndex)")
}
}
// Now change value of index 4 of array
array[4] = 10 //index: [4]
didSet and Getter in Swift array
Why use didSet? why not set?
var arr: [Display] {
set {
array = newValue
initialiseElements(arr: newValue)
}
get {
return self.array
}
}
How can I prevent the use of arr within the class?
You can't, swift doesn't have such access control
otherwise ensure we only initiliseElements(:) via calls outside the class
Again, you can't.
It makes no sense logically as well, think of what you are asking for, you are asking class to declare a variable which itself cant set (read only) but should expose to it outside class to write (read + write) it?
How is that possible? What is the use case you are trying to solve here? If for some reason you ended up with this solution, may be solutioning is wrong! re think of your implementation.
Hope this helps
removeAtIndex with didSet
The didSet method will be called automatically, if you do not want to execute the lines of code in didSet method, the below tricks may work for you.
Take
Bool
variable, and in the willSet observer method make it to true, in didSet method check for this bool variable's status and if true ignore, and immediately make that to false.In didSet observer method you will get the oldValue of the array and find the count, and compare with current count.
Swift didSet does not fire
Why is it so?
didSet
fires when you change value of the property, in example it would be when array itself changes.
Here
exerciseSets[index].isEnabled = !exerciseSets[index].isEnabled
you change property of some object in the array, but array stays the same, and contains same objects. So didSet
won't be called.
Can I somehow use the former option?
No, you can't. Use latter.
EDIT: I checked @martin-r advice to use structs for ExerciseSet
, and it works, didSet
set is called. Because when property value change for structure basically means creation of new structure with all but changed field being copied.
Filter array in its didSet
The error is due to the fact that as it says the result from the call to the function filter is unused. If you want to change the variable “activity” you should replace it with:
activity = activity.filter {!$0.isDone}
Related Topics
Background Animation with Depth in Spritekit
How to Code Initwithcoder in Swift
Convert Opengl Shader to Metal (Swift) to Be Used in Cifilter
Sub-Classing Nstextstorage Causes Significant Memory Issues
Firebase Query Containing Value
Round Top Corners of a Uiview in Swift
(Swift) Nstimer Stop When Scrolling
Swift Realm, Load the Pre-Populated Database the Right Way
Include an Extension for a Class Only If iOS11 Is Available
How to Change the Uinavigationbar Title's Position
Swiftui .Rotationeffect() Framing and Offsetting
Does Kotlin Has Extension Class to Interface Like Swift
Uisplitviewcontroller Displaymodebuttonitem()
How to Connect Localhost (With Invalid Certificate) Using Alamofire
Subclass Nsapplication in Swift
Is There Any Way of Locking an Object in Swift Like in C#