Has anyone done the challenge I want some explanation, please.
This is the text:
Challenge
Build a UI with a List and a Button below it.
Declare a property that contains an array of 5 strings. These can be any 5 words you want. (I did)
Each time the button is tapped, choose a random word from the array and put it in the list. (I programmed the button, but I couldn’t make it to choose from the array.
The number of items in the list should grow as you tap the button.
This is the code file contentView
struct ContentView: View {
var arrayinlist: [arrayof5] = [arrayof5 (word1: "Apple", word2: "Bus", word3: "Car", word4: "Door", word5: "Elephant")]
var body: some View {
List (arrayinlist) {word in
Text(word.word1)
Text(word.word2)
Text(word.word3)
Text(word.word4)
Text(word.word5)
}
Button(action: {deal()}, label: {
Image("button")
})
}
mutating func deal (){
var show = Int.random(in: 0...4)
arrayinlist = "word" + String(show)
}
file 2 arrayod5:
struct arrayof5: Identifiable {
var id :UUID = UUID()
var word1:String
var word2:String
var word3:String
var word4:String
var word5:String
}