-1
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.

Na el
  • 75
  • 9
  • 1
    You have to append an instance of `Gg`, so: `self.items.append(Gg(id: 99, task: text))`... `99` is just a placeholder for an actual `id`, based on whatever logic you have – New Dev Oct 14 '20 at 14:23

1 Answers1

1
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.

KevinP
  • 2,562
  • 1
  • 14
  • 27