2

I want to know about syntax of using enumerated with ForEach. I am using a customID.

Here is my code:

ForEach(arrayNew.enumerated(), id:\.customID) { (index, item) in

}

Update:

ForEach(Array(arrayNew.enumerated()), id:\.element.customID) { (index, item) in
    Text(String(index) + item)  
}
2
  • 2
    Does this answer your question stackoverflow.com/a/62384275/12299030? Or this stackoverflow.com/a/59863409/12299030? Commented Dec 30, 2020 at 19:33
  • thanks a lot, I do not understand why I should put my arrayNew in another Array? why? also please see my update for any improvement Commented Dec 30, 2020 at 19:44

1 Answer 1

4

Let's assume we have an Array of objects of type Item:

struct Item {
    let customID: Int
    let value: String
}

let arrayNew = [
    Item(customID: 1, value: "1"),
    Item(customID: 23, value: "12"),
    Item(customID: 2, value: "32")
]

Now, if we want to access both offset and item from the array, we need to use enumerated():

arrayNew.enumerated()

However, it returns an EnumeratedSequence (and not an Array):

@inlinable public func enumerated() -> EnumeratedSequence<Array<Element>>

If we take a look at the signature of ForEach, we can see that it expects RandomAccessCollection:

public struct ForEach<Data, ID, Content> where Data : RandomAccessCollection, ID : Hashable

The problem here is that EnumeratedSequence doesn't conform to RandomAccessCollection.

But Array does - we just need to convert the result of enumerated() back to an Array:

Array(arrayNew.enumerated())

Now, we can use it directly in the ForEach:

ForEach(Array(arrayNew.enumerated()), id: \.element.customID) { offset, item in
    Text("\(offset) \(item.customID) \(item.value)")
}
Sign up to request clarification or add additional context in comments.

4 Comments

That is good to understand why I have to use Array() but why we do not need it here: ** for (index, item) in <#T##items: Any...##Any#>.enumerated() { <#T##items: Any...##Any#> } **
@mimi This is because for-in loop requires conformance to Sequence - which applies to both Array and EnumeratedSequence. The standard for-in loop is completely different from SwiftUI ForEach.
so helpful, thanks again! My last but least question, when we use enumerated() in our codes, does put side effect in our app? is it make it expensive to run codes with enumerated method? I mean if I just use my codes without enumerated is it faster in run time?
@mimi As far as I know, you shouldn't see any difference.

Your Answer

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