Hello World ! I have an issue with a ForEach loop and a SwiftData Model
Here is the code :
import Foundation
import SwiftData
@Model
final class Todo {
var id: UUID
var content: String
var isDone: Bool
var isImportant: Bool
var sortValue: Int
init(content: String, isDone: Bool, isImportant: Bool) {
self.id = UUID()
self.content = content
self.isDone = isDone
self.isImportant = isImportant
if isImportant{
self.sortValue = 0
} else {
self.sortValue = 1
}
}
}
final class Tag {
var id: UUID
var content: String
init(content: String) {
self.id = UUID()
self.content = content
}
}
and the content view :
import SwiftUI
import SwiftData
struct AddTodoView: View {
@Environment(\.modelContext) private var modelContext
@Environment(\.dismiss) var dismiss
@Query private var tags: [Tag]
@State private var selectedTag: UUID
@State private var content: String = ""
@State private var isImportant: Bool = false
@State private var showAlert: Bool = false
@State private var showAchivement: Bool = false
var body: some View {
TextField("Add a Todo", text: $content)
.padding()
Toggle(isOn: $isImportant) {
Text("Important Todo")
}
.padding()
VStack{
Picker("Tag", selection: $selectedTag) {
ForEach(tags, id: \.self){ tag in
Text(tag.content)
}
}
}
and here is the issue message :
Referencing initializer 'init(_🆔 content:)' on 'ForEach' requires that 'Tag' conform to 'Hashable'