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:

Not sure how far you got, but I just did the challenge & was able to create unique ids for each element I was adding to the list by rewriting the items id with a new UUID every time I pulled a word.

import SwiftUI

struct WordView: View {
    
    var wordItems:[WordItem] = [WordItem(word: "Blue"),                                                           
                                WordItem(word:"Red"),
                                WordItem(word:"Yellow"),
                                WordItem(word:"Orange"),
                                WordItem(word:"Green")]
    
@State  var wordList:[WordItem] = []
    
    var body: some View {
        
        VStack {
            List(wordList) { wrd in
                
                Text(wrd.word)
            }
            
            Button("Add Random Word") {
                addWord()
                
            }
            
            .listStyle(.plain)
        }
    }
    
    func addWord(){
        //stores random word item from list of available word items
        var wValue = wordItems[Int.random(in: 0...4)]
        
        //assigns unique value to word item
        wValue.id = UUID()
        
        //adds new word to list
        wordList.append(wValue)
 
    }
}

#Preview {
    WordView()
}

@prodpoonk

Hi Derek,

Welcome to the community.

Whilst that solves the uniquely identifiable array element issue, you are applying advanced knowledge which is covered later in the courses by placing data in a struct with an id. I would imagine that your struct would have been something like:

struct WordItem: Identifiable {
    var id = UUID()
    var word: String
}

Thanks Chris! Yes that’s it! I had to do a little Google searching to figure it out, I figured I didn’t need to solve it but I was just curious.

Well done. There’s nothing wrong with trying to be a little bit ahead of the game. If you’ve already got a bit of programming experience, things do tend to fall into place a bit easier.