How to append an identifiable struct list?

181 Views Asked by At
struct Gg: Identifiable{
    
    let id: Int
    let task: String
}

struct ContentView: View {

@State private var items = [Gg(id: 1, task:"take the trash out"), Gg(id: 2, task:"Go for a run")]

var body: some View {

AddTaskUIView(title: "Add Item", isShown: $isPresented, text: $text, onDone: { text in
                   
                   
                    self.items.append(text)
                    self.text = ""
                    })

}}

I am getting an error

Cannot convert value of type 'String' to expected argument type 'Gg'

how to I append to the task of the structure Gg? I am new to swift so I would appriacte any help thanks.

1

There are 1 best solutions below

0
On BEST ANSWER
struct Gg: Identifiable{
    let id: UUID = UUID()
    let task: String

    init(_ task: String) {
        self.task = task
    }
}
...
@State private var items = [Gg("take the trash out"), Gg("Go for a run")]
...
self.items.append(Gg(text))

Heres a solution that does not have the problem of keeping track of the id yourself.