Outputting array items in grid

Hi. I'm new to swift and IOS development. I've just been messing around with swift just to get a better understanding of it and I'm a bit confused on something. I have the following code in my app.

  let arr = [1,2,3,4,5,6,7,8,9,2,3,5]
   
  var lo: [GridItem] = [
    GridItem(.adaptive(minimum: 3, maximum: 10)),
    GridItem(.adaptive(minimum: 3, maximum: 10)),
    GridItem(.adaptive(minimum: 3, maximum: 10))
  ]
   
  var body: some View {
    ScrollView {
      LazyHGrid(rows: lo, spacing: 50, content: {
        ForEach(arr, id: \.self) { item in
          Text(String(item)).padding()
        }
      })
    }
  }

I'm expecting it to display a grid displaying every item in the array arr, but it only displays the items up to the 9th element. If someone could explain why this is, that would be great. Here is a screenshot of the output:

Here is an example program with a display_grid() function that takes as arguments an array of int , the size of the array, and the desired size ... Array is a data structure that contains a group [of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in co uter programs to organize data so that a related set of values can be easily sorted or search https://spacebarcounter.org/

The problem is that you have the same elements twice. Hence id: \.self is confused.

That's was the log says: LazyHGridLayout: the ID 5 is used by multiple child views, this will give undefined results!

If you replace by

let arr = [1,2,3,4,5,6,7,8,9,12,13,15]

you get them all.

Or you have to build a specific id for array elements that is unique.

Thanks. I get it now. So how can I make the grid output the same values?

Outputting array items in grid
 
 
Q