Module 2: Lesson 5 Challenge Console Warning?

I did the challenge but at the console this message is showed

ForEach<Array, String, Text>: the ID jabuticaba occurs multiple times within the collection, this will give undefined results!

ForEach<Array, String, Text>: the ID pêra occurs multiple times within the collection, this will give undefined results!

ForEach<Array, String, Text>: the ID maça occurs multiple times within the collection, this will give undefined results!

ForEach<Array, String, Text>: the ID banana occurs multiple times within the collection, this will give undefined results!

ForEach<Array, String, Text>: the ID uva occurs multiple times within the collection, this will give undefined results!

As a beginner I don’t know if its good or not :confused: and I’m running on my iPhone.


//
//  ContentView.swift
//  challenge_2
//
//  Created by Waldemar Osmala on 1/24/24.
//

import SwiftUI

struct ContentView: View {
    
    var arrayOf5 = ["maça","banana","pêra","uva","jabuticaba"]
    
    @State var listItems = [String]()
        
    var body: some View {
        
        
        
        VStack {
            
            List(listItems, id: \.self) { items in
              Text(items)
                }
            
            Button("Add Items") {
                if let itemPicked = arrayOf5.randomElement() {
                    
                    listItems.append(itemPicked)

                }
            }
        }
        
    }
}

#Preview {
    ContentView()
}

SwiftUI requires items in a List to be uniquely identifiable so what is happening is that you are getting repeating elements from the arrayOf5 appearing in the List and that’s what the warning message is referring to.

For the time being just ignore the message in the console. In time you will learn about how to make items in a List uniquely identifiable and then it will all make sense.

1 Like

Tks Chris :+1: