I have a tasks app, where I want to remove the task when the user taps the done button. I want to remove the task the user tapped 'done' but idk how, can someone help mii?
Note that I have an outdated Mac running macOS 12 & Xcode 13 so not everything up to date is possible.
Simplified version of the code follows:
import SwiftUI
struct Task: Identifiable {
let id = UUID()
var name: String
}
struct ContentView: View {
@State var tasks = [
Task(name: "task 1"),
Task(name: "task 2"),
Task(name: "task 3")
]
var body: some View {
List {
ForEach(tasks) { task in
HStack {
Text(task.name)
Spacer()
Button("Done!") {
tasks.remove(at: 0) //idk how to remove :(
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Welcome to the forum.
You remove at : 0, so you remove always the first element.
Change with this:
Button("Done!") {
tasks.removeAll(where: {$0.name == task.name }) // (at: 0) //idk how to remove :(
}
If OK, don't forget to close the thread by marking this answer as correct. Otherwise explain what problem you have. Good continuation.