Post

Replies

Boosts

Views

Activity

Reply to Same content in 2 cards
I had a similar response in Xcode debug area where I got the following message: ForEach<Array, Int, Text>: the ID 4 occurs multiple times within the collection, this will give undefined results! for the following code: import SwiftUI struct ContentView: View { let numbers = [1, 2, 3, 4, 4, 5] var body: some View { List { ForEach(numbers, id: \.self) { number in Text(String(number)) } } } } #Preview { ContentView() } In fact, the behavior is exactly what someone would expect to see: Although I understood @Claude31's solution of adding a differentiating property for the same emojis or using UUID(), I am still confused about the following points: Why does passing the array elements that hold the same value create issues in the first place? It seems quite straightforward to print each number one by one and it worked for the case of integers anyway. What is the main reason why Xcode is not happy with the array elements of the same value? What if our goal is to loop an array of the elements of which must have the same value? How can we tackle that?
Mar ’24