We have a class with an array property that has a didSet observer. However, it seems that when we call removeAtIndex on this array, the didSet observer is called. Is there any way we can stop this from happening?
var items: [String] = []
{
didSet
{
println(self.items.count)
}
}
...
func removeIndex(index: Int)
{
self.items.removeAtIndex(index)
// now didSet is called, but we don't want that
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With