Get index in ForEach in SwiftUI : <p>I have an array and I want to iterate through it initialize views based on array value, and want to perform action based on array item index</p>

<p>When I iterate through objects</p>

<pre class="lang-swift prettyprint-override"><code>ForEach(array, id: \.self) { item in
  CustomView(item: item)
    .tapAction {
      self.doSomething(index) // Can't get index, so this won't work
    }
}
</code></pre>

<p>So, I've tried another approach</p>

<pre class="lang-swift prettyprint-override"><code>ForEach((0..&lt;array.count)) { index in
  CustomView(item: array[index])
    .tapAction {
      self.doSomething(index)
    }
}
</code></pre>

<p>But the issue with second approach is, that when I change array, for example, if <code>doSomething</code> does following</p>

<pre><code>self.array = [1,2,3]
</code></pre>

<p>views in <code>ForEach</code> do not change, even if values are changed. I believe, that happens because <code>array.count</code> haven't changed. </p>

<p>Is there a solution for this? Thanks in advance.</p>
