Get index in ForEach in SwiftUI - swift

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
When I iterate through objects
ForEach(array, id: \.self) { item in
CustomView(item: item)
.tapAction {
self.doSomething(index) // Can't get index, so this won't work
}
}
So, I've tried another approach
ForEach((0..<array.count)) { index in
CustomView(item: array[index])
.tapAction {
self.doSomething(index)
}
}
But the issue with second approach is, that when I change array, for example, if doSomething does following
self.array = [1,2,3]
views in ForEach do not change, even if values are changed. I believe, that happens because array.count haven't changed.
Is there a solution for this?

Another approach is to use:
enumerated()
ForEach(Array(array.enumerated()), id: \.offset) { index, element in
// ...
}
Source: https://alejandromp.com/blog/swiftui-enumerated/

This works for me:
Using Range and Count
struct ContentView: View {
#State private var array = [1, 1, 2]
func doSomething(index: Int) {
self.array = [1, 2, 3]
}
var body: some View {
ForEach(0..<array.count) { i in
Text("\(self.array[i])")
.onTapGesture { self.doSomething(index: i) }
}
}
}
Using Array's Indices
The indices property is a range of numbers.
struct ContentView: View {
#State private var array = [1, 1, 2]
func doSomething(index: Int) {
self.array = [1, 2, 3]
}
var body: some View {
ForEach(array.indices) { i in
Text("\(self.array[i])")
.onTapGesture { self.doSomething(index: i) }
}
}
}

I usually use enumerated to get a pair of index and element with the element as the id
ForEach(Array(array.enumerated()), id: \.element) { index, element in
Text("\(index)")
Text(element.description)
}
For a more reusable component, you can visit this article https://onmyway133.com/posts/how-to-use-foreach-with-indices-in-swiftui/

I needed a more generic solution, that could work on all kind of data (that implements RandomAccessCollection), and also prevent undefined behavior by using ranges.
I ended up with the following:
public struct ForEachWithIndex<Data: RandomAccessCollection, ID: Hashable, Content: View>: View {
public var data: Data
public var content: (_ index: Data.Index, _ element: Data.Element) -> Content
var id: KeyPath<Data.Element, ID>
public init(_ data: Data, id: KeyPath<Data.Element, ID>, content: #escaping (_ index: Data.Index, _ element: Data.Element) -> Content) {
self.data = data
self.id = id
self.content = content
}
public var body: some View {
ForEach(
zip(self.data.indices, self.data).map { index, element in
IndexInfo(
index: index,
id: self.id,
element: element
)
},
id: \.elementID
) { indexInfo in
self.content(indexInfo.index, indexInfo.element)
}
}
}
extension ForEachWithIndex where ID == Data.Element.ID, Content: View, Data.Element: Identifiable {
public init(_ data: Data, #ViewBuilder content: #escaping (_ index: Data.Index, _ element: Data.Element) -> Content) {
self.init(data, id: \.id, content: content)
}
}
extension ForEachWithIndex: DynamicViewContent where Content: View {
}
private struct IndexInfo<Index, Element, ID: Hashable>: Hashable {
let index: Index
let id: KeyPath<Element, ID>
let element: Element
var elementID: ID {
self.element[keyPath: self.id]
}
static func == (_ lhs: IndexInfo, _ rhs: IndexInfo) -> Bool {
lhs.elementID == rhs.elementID
}
func hash(into hasher: inout Hasher) {
self.elementID.hash(into: &hasher)
}
}
This way, the original code in the question can just be replaced by:
ForEachWithIndex(array, id: \.self) { index, item in
CustomView(item: item)
.tapAction {
self.doSomething(index) // Now works
}
}
To get the index as well as the element.
Note that the API is mirrored to that of SwiftUI - this means that the initializer with the id parameter's content closure is not a #ViewBuilder.
The only change from that is the id parameter is visible and can be changed

For non zero based arrays avoid using enumerated, instead use zip:
ForEach(Array(zip(items.indices, items)), id: \.0) { index, item in
// Add Code here
}

I created a dedicated View for this purpose:
struct EnumeratedForEach<ItemType, ContentView: View>: View {
let data: [ItemType]
let content: (Int, ItemType) -> ContentView
init(_ data: [ItemType], #ViewBuilder content: #escaping (Int, ItemType) -> ContentView) {
self.data = data
self.content = content
}
var body: some View {
ForEach(Array(zip(data.indices, data)), id: \.0) { idx, item in
content(idx, item)
}
}
}
Now you can use it like this:
EnumeratedForEach(items) { idx, item in
...
}

ForEach is SwiftUI isn’t the same as a for loop, it’s actually doing something called structural identity. The documentation of ForEach states:
/// It's important that the `id` of a data element doesn't change, unless
/// SwiftUI considers the data element to have been replaced with a new data
/// element that has a new identity.
This means we cannot use indices, enumerated or a new Array in the ForEach. The ForEach must be given the actual array of identifiable items. This is so SwiftUI can animate the rows around to match the data, obviously this can't work with indicies, e.g. if row at 0 is moved to 1 its index is still 0.
To solve your problem of getting the index, you simply have to look up the index like this:
ForEach(items) { item in
CustomView(item: item)
.tapAction {
if let index = array.firstIndex(where: { $0.id == item.id }) {
self.doSomething(index)
}
}
}
You can see Apple doing this in their Scrumdinger sample app tutorial.
guard let scrumIndex = scrums.firstIndex(where: { $0.id == scrum.id }) else {
fatalError("Can't find scrum in array")
}

The advantage of the following approach is that the views in ForEach even change if state values ​​change:
struct ContentView: View {
#State private var array = [1, 2, 3]
func doSomething(index: Int) {
self.array[index] = Int.random(in: 1..<100)
}
var body: some View {
let arrayIndexed = array.enumerated().map({ $0 })
return List(arrayIndexed, id: \.element) { index, item in
Text("\(item)")
.padding(20)
.background(Color.green)
.onTapGesture {
self.doSomething(index: index)
}
}
}
}
... this can also be used, for example, to remove the last divider
in a list:
struct ContentView: View {
init() {
UITableView.appearance().separatorStyle = .none
}
var body: some View {
let arrayIndexed = [Int](1...5).enumerated().map({ $0 })
return List(arrayIndexed, id: \.element) { index, number in
VStack(alignment: .leading) {
Text("\(number)")
if index < arrayIndexed.count - 1 {
Divider()
}
}
}
}
}

2021 solution if you use non zero based arrays avoid using enumerated:
ForEach(array.indices,id:\.self) { index in
VStack {
Text(array[index].name)
.customFont(name: "STC", style: .headline)
.foregroundColor(Color.themeTitle)
}
}
}

To get indexing from SwiftUI's ForEach loop, you could use closure's shorthand argument names:
#State private var cars = ["Aurus","Bentley","Cadillac","Genesis"]
var body: some View {
NavigationView {
List {
ForEach(Array(cars.enumerated()), id: \.offset) {
Text("\($0.element) at \($0.offset) index")
}
}
}
}
Results:
// Aurus at 0 index
// Bentley at 1 index
// Cadillac at 2 index
// Genesis at 3 index
P. S.
Initially, I posted an answer with a "common" expression that all Swift developers are used to, however, thanks to #loremipsum I changed it. As stated in WWDC 2021 Demystify SwiftUI video (time 33:40), array indices are not stable from \.self identity (key path).
ForEach(0 ..< cars.count, id: \.self) { // – NOT STABLE
Text("\(cars[$0]) at \($0) index")
}

Here is a simple solution though quite inefficient to the ones above..
In your Tap Action, pass through your item
.tapAction {
var index = self.getPosition(item)
}
Then create a function the finds the index of that item by comparing the id
func getPosition(item: Item) -> Int {
for i in 0..<array.count {
if (array[i].id == item.id){
return i
}
}
return 0
}

You can use this method:
.enumerated()
From the Swift documentation:
Returns a sequence of pairs (n, x), where n represents a consecutive
integer starting at zero and x represents an element of the sequence.
var elements: [String] = ["element 1", "element 2", "element 3", "element 4"]
ForEach(Array(elements.enumerated()), id: \.element) { index, element in
Text("\(index) \(element)")
}

Just like they mentioned you can use array.indices for this purpose
BUT remember that indexes that you've got are started from last element of array, To fix this issue you must use this: array.indices.reversed() also you should provide an id for the ForEach.
Here's an example:
ForEach(array.indices.reversed(), id:\.self) { index in }

Related

Get the two foreach array the top one's index?

is that a way to get the top parten foreach's index? by two using foreach array?
ForEach(total, id: \.id){ item in
ForEach(nameber, id: \.id){ item in
(get the total;s index)
}
}
very criptic question. Try this, to get the index and the item of the outer ForEach:
ForEach(Array(total.enumerated()), id: \.1) { index, item in
ForEach(member, id: \.id) { item2 in
Text(String(index) + " \(item)" + " \(item2)")
}
}
You need to give the content's input different variable names.
You have both of them as item, so if you have different names, you can access the outside variable - not just the inside one.
Example:
struct ContentView: View {
var body: some View {
ForEach(0 ..< 5) { i in
ForEach(0 ..< 5) { j in
Text("(i: \(i), j: \(j))")
}
}
}
}
Result:
Variables renamed to match your example (using implicit (id: \.id, assuming they conform to Identifiable):
struct ContentView: View {
var body: some View {
ForEach(total) { totalItem in
ForEach(nameber) { nameberItem in
Text("(totalItem: \(totalItem), nameberItem: \(nameberItem))")
}
}
}
}

SwiftUI memory leak

I'm getting a weird memory leak in SwiftUI when using List and id: \.self, where only some of the items are destroyed. I'm using macOS Monterey Beta 5.
Here is how to reproduce:
Create a new blank SwiftUI macOS project
Paste the following code:
class Model: ObservableObject {
#Published var objs = (1..<100).map { TestObj(text: "\($0)")}
}
class TestObj: Hashable {
let text: String
static var numDestroyed = 0
init(text: String) {
self.text = text
}
static func == (lhs: TestObj, rhs: TestObj) -> Bool {
return lhs.text == rhs.text
}
func hash(into hasher: inout Hasher) {
hasher.combine(text)
}
deinit {
TestObj.numDestroyed += 1
print("Deinit: \(TestObj.numDestroyed)")
}
}
struct ContentView: View {
#StateObject var model = Model()
var body: some View {
NavigationView {
List(model.objs, id: \.self) { obj in
Text(obj.text)
}
Button(action: {
var i = 1
model.objs.removeAll(where: { _ in
i += 1
return i % 2 == 0
})
}) {
Text("Remove half")
}
}
}
}
Run the app, and press the "Remove half" button. Keep pressing it until all the items are gone. However, if you look at the console, you'll see that only 85 items have been destroyed, while there were 99 items. The Xcode memory graph also supports this.
This seems to be caused by the id: \.self line. Removing it and switching it out for id: \.text fixes the problem.
However the reason I use id: \.self is because I want to support multiple selection, and I want the selection to be of type Set<TestObj>, instead of Set<UUID>.
Is there any way to solve this issue?
If you didn't have to use selection in your List, you could use any unique & constant id, for example:
class TestObj: Hashable, Identifiable {
let id = UUID()
/* ... */
}
And then your List with the implicit id: \.id:
List(model.objs) { obj in
Text(obj.text)
}
This works great. It works because now you are no longer identifying the rows in the list by a reference type, which is kept by SwiftUI. Instead you are using a value type, so there aren't any strong references causing TestObjs to not deallocate.
But you need selection in List, so see more below about how to achieve that.
To get this working with selection, I will be using OrderedDictionary from Swift Collections. This is so the list rows can still be identified with id like above, but we can quickly access them. It's partially a dictionary, and partially an array, so it's O(1) time to access an element by a key.
Firstly, here is an extension to create this dictionary from the array, so we can identify it by its id:
extension OrderedDictionary {
/// Create an ordered dictionary from the given sequence, with the key of each pair specified by the key-path.
/// - Parameters:
/// - values: Every element to create the dictionary with.
/// - keyPath: Key-path for key.
init<Values: Sequence>(_ values: Values, key keyPath: KeyPath<Value, Key>) where Values.Element == Value {
self.init()
for value in values {
self[value[keyPath: keyPath]] = value
}
}
}
Change your Model object to this:
class Model: ObservableObject {
#Published var objs: OrderedDictionary<UUID, TestObj>
init() {
let values = (1..<100).map { TestObj(text: "\($0)")}
objs = OrderedDictionary<UUID, TestObj>(values, key: \.id)
}
}
And rather than model.objs you'll use model.objs.values, but that's it!
See full demo code below to test the selection:
struct ContentView: View {
#StateObject private var model = Model()
#State private var selection: Set<UUID> = []
var body: some View {
NavigationView {
VStack {
List(model.objs.values, selection: $selection) { obj in
Text(obj.text)
}
Button(action: {
var i = 1
model.objs.removeAll(where: { _ in
i += 1
return i % 2 == 0
})
}) {
Text("Remove half")
}
}
.onChange(of: selection) { newSelection in
let texts = newSelection.compactMap { selection in
model.objs[selection]?.text
}
print(texts)
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
EditButton()
}
}
}
}
}
Result:

Index out of range when overwriting array that UI depends on

I have a ForEach, that iterates over array of indices, accessing array's data by subscript, at some point in loading program from file process this array needs to be rewritten, thus its size is changed, but when the size of already rendered cells exceeds the new size the
Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range occurs.
ForEach(computer.program.commands.indices, id: \.self) { i in
GeometryReader { cell in
CommandCellView($computer.program[i],
computer.program[i].number == computer.commandCounter.getValue())
.renderIfWillBeSeen(preload, viewGeometry: cell, generalGeometry: g)
}.frame(height: cellHeight)
}
the computer is #StateObject, and everything inside of it is a struct
tried to call file method inside withAnimation() didn't work
works perfectly fine when rendered cell count is smaller than new array count
So, initial problem was to interate over binding of a collection in ForEach as if it was a collection of bindings. That functionality will be added in the new version of swift, however, I decided to post the solution that I found, in case someone will need that type of functionality in an older version of SwiftUI
API
//$computer.program - Binding<MutableCollection<Command>>
//command - Binding<Command>
BindingForEach($computer.program) { command in
CommandCellView(...,
command: command)
}
BindingForEach.swift
import SwiftUI
struct BindingForEach<Data: MutableCollection, Content: View>: DynamicViewContent where Data.Index: Hashable,
Data.Indices == Range<Int> {
#Binding public var data: Data
private var builder: (Binding<Data.Element>) -> Content
private var elementBindings = FunctionCache<Data.Index, Binding<Data.Element>>()
init(_ collection: Binding<Data>, #ViewBuilder _ content: #escaping (Binding<Data.Element>) -> Content) {
self._data = collection
self.builder = content
self.elementBindings = FunctionCache<Data.Index, Binding<Data.Element>> { [self] (i) in
Binding<Data.Element> { [self] in
self.data[i]
} set: { [self] in
self.data[i] = $0
}
}
}
var body: some View {
ForEach(data.enumerated().map({ (i, _) in i }), id: \.self) { i in
builder(elementBindings[i])
}
}
}
FunctionCache.swift
It it important to cache created bindings, to optimize the performance
import Foundation
class FunctionCache<TSource: Hashable, TResult> {
private let function: (TSource) -> (TResult)
private var cache = [TSource : TResult]()
init (_ function: #escaping (TSource) -> TResult = { _ in fatalError("Unspecified function") }) {
self.function = function
}
subscript(_ i: TSource) -> TResult {
get {
if !cache.keys.contains(i) {
cache[i] = function(i)
}
return cache[i]!
}
}
}

How to increment a state in a ForEach loop in SwiftUI?

I need to increment the genNum variable and pass it to another struct in a ForEach loop. My code is compiling properly but cannot preview it in the simulation as well as the canvas. Getting "cannot preview in this file". Another error I am getting is "RemoteHumanReadableError: The operation couldn’t be completed. (BSServiceConnectionErrorDomain error 3.)".
BSServiceConnectionErrorDomain (3):
==BSErrorCodeDescription: OperationFailed
import SwiftUI
#available(iOS 14.0, *)
struct CategoryView: View {
#State var genNum: Int = -1
var categories: [Int: [PokemonData]] {
Dictionary(
grouping: pokemonData,
by: { $0.generation }
)
}
var columns: [GridItem] = [
GridItem(.fixed(170)),
GridItem(.fixed(170))
]
var body: some View {
NavigationView {
ScrollView {
LazyVGrid(columns: columns) {
ForEach(categories.keys.sorted(), id: \.self) { key in
CategoryCard(genNum: self.increment()) // <--- Having problem with this line
}
}
AllCard()
.padding(.horizontal, 15)
.padding(.bottom, 20)
}
.navigationTitle("Categories")
}
}
// Function to increment the state value
func increment() -> Int {
self.genNum += 1
let i = genNum
return i
}
}
If all you want to do is to pass values from 0 to count-1 to your CategoryCard() initializer, you can use the enumerated() function. That function takes an array as input and returns an array of tuples where the first item is an array index and the 2nd item is the the element from the original array.
This code, for example:
let array = ["zero", "one", "two", "three"]
array.enumerated().forEach { (index, string) in
print (index, string)
}
Outputs the following:
0 zero
1 one
2 two
3 three
So you could rewrite your code like this:
LazyVGrid(columns: columns) {
ForEach(categories.keys.sorted().enumerated(), id: \.self) { (index, key) in
CategoryCard(genNum: index) // <--- Having problem with this line
}
}
(Disclaimer: I haven't used SwiftUI yet, so I'm not totally clear on what ForEach is doing inside your LazyGrid)

IndexSet referring to index of the section instead of the row

I want to implement a swipe-to-delete functionality on a SwiftUI Section by using the .onDelete modifier. The problem is that it always deletes the first item in the list.
My view has a list with dynamic sections created with a ForEach.
struct SetListView : View {
var setlist: Setlist
var body : some View {
List {
ForEach(setlist.sets) {
SetSection(number: $0.id, songs: $0.songs)
}
}
.listStyle(.grouped)
}
}
In each section, there is another ForEach to create the dynamic rows:
private struct SetSection : View {
var number: Int
#State var songs: [Song]
var body : some View {
Section (header: Text("Set \(number)"), footer: Spacer()) {
ForEach(songs) { song in
SongRow(song: song)
}
.onDelete { index in
self.songs.remove(at: index.first!)
}
}
}
}
While debugging, I found out that the IndexSet is referring to the current section instead of the row. So when deleting items from the first section, always the first item gets deleted (as the index for the first section is 0).
Is this a bug in SwiftUI?
If not, then how could I get the index for the row?
In simple terms, a solution to this problem is to pass the section to your deletion method by:
Adopting RandomAccessCollection on your source data.
Binding the section in your outer ForEach and then using it in your inner ForEach, passing it to your deletion method:
List {
ForEach(someGroups.indices) { section in
bind(self.someGroups[section]) { someGroup in
Section(header: Text(someGroup.displayName)) {
ForEach(someGroup.numbers) { number in
Text("\(number)")
}
.onDelete { self.delete(at: $0, in: section) }
}
}
}
}
func delete(at offsets: IndexSet, in section: Int) {
print("\(section), \(offsets.first!)")
}
A complete, contrived working example
(Also available in Gist form for convenience):
import SwiftUI
func bind<Value, Answer>(_ value: Value, to answer: (Value) -> Answer) -> Answer { answer(value) }
struct Example: View {
struct SomeGroup: Identifiable, RandomAccessCollection {
typealias Indices = CountableRange<Int>
public typealias Index = Int;
var id: Int
var displayName: String
var numbers: [Int]
public var endIndex: Index {
return numbers.count - 1
}
public var startIndex: Index {
return 0
}
public subscript(position: Int) -> Int {
get { return numbers[position] }
set { numbers[position] = newValue }
}
}
var someGroups: [SomeGroup] = {
return [
SomeGroup(id: 0, displayName: "First", numbers: [1, 2, 3, 4]),
SomeGroup(id: 1, displayName: "Second", numbers: [1, 3, 5, 7])
]
}()
var body: some View {
List {
ForEach(someGroups.indices) { section in
bind(self.someGroups[section]) { someGroup in
Section(header: Text(someGroup.displayName)) {
ForEach(someGroup.numbers) { number in
Text("\(number)")
}
.onDelete { self.delete(at: $0, in: section) }
}
}
}
}
.listStyle(.grouped)
}
func delete(at offsets: IndexSet, in section: Int) {
print("\(section), \(offsets.first!)")
}
}
Many thanks to #rob-mayoff who pointed me in the right direction for this solution via Twitter!
I had exactly the same problem. Turns out, SwiftUI's (current?) implementation does not recognize nested lists. This means that each SetSection in your List is interpreted as a single row even though you have a ForEach in it with the actual SongRows. Hence, it the IndexSet (index.first!) always returns zero.
What I've also noticed is that even with a flat hierarchy such as..
List {
Section {
ForEach(...) {
...
}
}
Section {
ForEach(...) {
...
}
}
}
..individual rows cannot be moved between sections. This is also true when directly using two ForEach, i.e. without the Section wrappers.
We should probably file a report for each phenomenon.
It seems to work fine on Xcode 12.5
I'm using it like this:
struct Sections: View {
var items: [SomeData]
private var sections: [Date: [SomeData]] {
Dictionary(grouping: items, by: { $0.date })
}
private var headers: [Date] {
sections.map({ $0.key }).sorted().reversed()
}
var body: some View {
List {
ForEach(headers, id: \.self) { date in
Section(header: Text(date.friendly) {
AList(items: sections[date]!)
}
}
}
}
}
struct AList: View {
var items: [SomeData]
var body: some View {
ForEach(items) { data in
...
}
.onDelete(perform: delete)
}
private func delete(at offsets: IndexSet) {
// You can use `items.remove(atOffsets: offsets)`
for offset in offsets {
let data = items[offset]
print("\(data)")
// You can check here that this is the item that you want to remove and then you need to remove it from your data source.
// I'm using Realm and #Published vars that works fine, you should adapt to your logic.
}
}
}