Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removeAtIndex with didSet

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
}
like image 521
Qiviut Avatar asked May 02 '15 19:05

Qiviut


1 Answers

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.

  1. 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.

  2. In didSet observer method you will get the oldValue of the array and find the count, and compare with current count.

like image 186
Amit89 Avatar answered Sep 27 '22 23:09

Amit89