From 52f37b494edcaf868ac463b9a865ec6c7ee0b09c Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Mon, 5 Apr 2021 00:46:44 -0700 Subject: [PATCH] Adds the ability to delete a monster from your library. --- iOS/MonsterCards/Views/Library.swift | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/iOS/MonsterCards/Views/Library.swift b/iOS/MonsterCards/Views/Library.swift index 99c9597..09394f1 100644 --- a/iOS/MonsterCards/Views/Library.swift +++ b/iOS/MonsterCards/Views/Library.swift @@ -19,10 +19,26 @@ struct Library: View { var body: some View { NavigationView{ - List(allMonsters) { monster in - NavigationLink(destination: MonsterDetailWrapper(monster: monster)) { - Text(monster.name ?? "") + List { + ForEach(allMonsters) { monster in + 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") .navigationBarTitleDisplayMode(.inline)