Adds the ability to delete a monster from your library.
This commit is contained in:
@@ -19,10 +19,26 @@ struct Library: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationView{
|
NavigationView{
|
||||||
List(allMonsters) { monster in
|
List {
|
||||||
NavigationLink(destination: MonsterDetailWrapper(monster: monster)) {
|
ForEach(allMonsters) { monster in
|
||||||
Text(monster.name ?? "")
|
NavigationLink(destination: MonsterDetailWrapper(monster: monster)) {
|
||||||
|
Text(monster.name ?? "")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
.onDelete(perform: { indexSet in
|
||||||
|
for index in indexSet {
|
||||||
|
let monster = allMonsters[index]
|
||||||
|
viewContext.delete(monster)
|
||||||
|
do {
|
||||||
|
try viewContext.save()
|
||||||
|
} catch {
|
||||||
|
// Replace this implementation with code to handle the error appropriately.
|
||||||
|
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
||||||
|
let nsError = error as NSError
|
||||||
|
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
.navigationTitle("Library")
|
.navigationTitle("Library")
|
||||||
.navigationBarTitleDisplayMode(.inline)
|
.navigationBarTitleDisplayMode(.inline)
|
||||||
|
|||||||
Reference in New Issue
Block a user