3

Suppose I have an array:

var intArray: [Int] = [1,2,3,4,5] {
    didSet{
        //print index of value that was modified
    }
}

if I do intArray[2] = 10, what can I write inside didSet in order to print the index of the modified value (2, in this case) ?

6
  • 1
    This is not possible, with didSet you can only get newValue(array) and oldValue(array). Commented Jan 4, 2017 at 5:46
  • really? is there some workaround for that? Commented Jan 4, 2017 at 5:47
  • 2
    There's no built-in Swift way to do this, although you can write a custom solution like this: stackoverflow.com/a/27675375/6658553 Commented Jan 4, 2017 at 5:50
  • @Daniel You can write a wrapper for Array that forwards on all method calls, but intercepts the index as desired Commented Jan 4, 2017 at 5:51
  • 1
    @nathan answer satisfied me :) is not exactly what I needed but my program was similar and I could adapt my code easily Commented Jan 4, 2017 at 5:59

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting idea. Of course this will only work for in-place modifications and not so well for insertions and deletions, as every element after the insertion/deletion would be considered changed.
Agreed, and it also will not signal deleted element indexes. It does work in the limited scope of the OP's stated requirement (which he may find is but one use case in the long run).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.