如何在列表中插入结构化的可识别数组?
how to insert a structured identifiable array in a list?
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 {
NavigationView {
ZStack(alignment: .center) {
VStack {
List(self.items, id: \.self) { index in
Text("\(index)")
}
}}
我收到以下错误
无法声明名为“$id”的实体; '$' 前缀保留用于隐式合成声明
初始化器 'init(_:id:rowContent:)' 要求 'Gg' 符合 'Hashable'
退出新手,开始感谢帮助
你按项目迭代,而不是按索引,所以它很简单
VStack {
List(self.items) { item in
Text(item.task)
}
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 {
NavigationView {
ZStack(alignment: .center) {
VStack {
List(self.items, id: \.self) { index in
Text("\(index)")
}
}}
我收到以下错误
无法声明名为“$id”的实体; '$' 前缀保留用于隐式合成声明 初始化器 'init(_:id:rowContent:)' 要求 'Gg' 符合 'Hashable'
退出新手,开始感谢帮助
你按项目迭代,而不是按索引,所以它很简单
VStack {
List(self.items) { item in
Text(item.task)
}