Swift 如何附加可识别的结构列表?

Swift 如何附加可识别的结构列表?,swift,swiftui,swift-structs,Swift,Swiftui,Swift Structs,我犯了一个错误 无法将“String”类型的值转换为预期的参数类型“Gg” 如何附加到结构Gg的任务? 我是swift的新手,所以我会感谢您的帮助 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

我犯了一个错误

无法将“String”类型的值转换为预期的参数类型“Gg”

如何附加到结构Gg的任务? 我是swift的新手,所以我会感谢您的帮助

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 = ""
                    })

}}

这里有一个解决方案,它不存在自己跟踪id的问题。

您必须附加
Gg
的实例,因此:
self.items.append(Gg(id:99,task:text))
99
只是实际
id
的占位符,基于您拥有的任何逻辑
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))