From 6cd7dae53825d5c66b5c9c5cfc22a07210230d5b Mon Sep 17 00:00:00 2001 From: Avery Pace Date: Sun, 7 Nov 2021 18:12:21 -0500 Subject: [PATCH] Fix swift previews crash --- .../xcschemes/xcschememanagement.plist | 8 -------- Toki Trainer/Constants.swift | 8 ++++++++ Toki Trainer/Views/FlashCardResultsView.swift | 16 +++------------- Toki Trainer/Views/FlashCardView.swift | 2 +- 4 files changed, 12 insertions(+), 22 deletions(-) diff --git a/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcschemes/xcschememanagement.plist b/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcschemes/xcschememanagement.plist index 611d488..a3b04aa 100644 --- a/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcschemes/xcschememanagement.plist @@ -10,13 +10,5 @@ 0 - SuppressBuildableAutocreation - - 7E943A1C273211C200E7DDF4 - - primary - - - diff --git a/Toki Trainer/Constants.swift b/Toki Trainer/Constants.swift index ce3f85b..99606dc 100644 --- a/Toki Trainer/Constants.swift +++ b/Toki Trainer/Constants.swift @@ -7,6 +7,7 @@ import Foundation import UIKit +import CoreData struct K { static let posColors = [ @@ -23,4 +24,11 @@ struct K { "oth": UIColor.systemBrown, "extra": UIColor.systemBrown ] + + static var getFlashCardAnswersFetchRequest: NSFetchRequest { + let request: NSFetchRequest = FlashCardAnswer.fetchRequest() + request.sortDescriptors = [] + + return request + } } diff --git a/Toki Trainer/Views/FlashCardResultsView.swift b/Toki Trainer/Views/FlashCardResultsView.swift index 9f10aab..92ca657 100644 --- a/Toki Trainer/Views/FlashCardResultsView.swift +++ b/Toki Trainer/Views/FlashCardResultsView.swift @@ -10,13 +10,12 @@ import SwiftUI struct FlashCardResultsView: View { @Environment(\.managedObjectContext) private var viewContext - @FetchRequest(entity:FlashCardAnswer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \FlashCardAnswer.word, ascending: false)], predicate: NSPredicate(format: "word == %@", "a")) var flashCardAnswers: FetchedResults + @FetchRequest(fetchRequest: K.getFlashCardAnswersFetchRequest) var answers: FetchedResults @State private var statistics = 0.0 func calculateStatistics() { - print("count: \(flashCardAnswers.count)") - for answer in flashCardAnswers { + for answer in answers { if answer.triesCount != 0 { print("word: \(answer.word)") print("tries: \(answer.triesCount)") @@ -25,16 +24,7 @@ struct FlashCardResultsView: View { } } } - -// func calculateStatistics() { -// var correctAnswers = 0 -// for answer in flashCardAnswers { -// if answer.correct { -// correctAnswers += 1 -// } -// self.statistics = Double(correctAnswers) / Double(flashCardAnswers.count) -// } -// } + var body: some View { Text("Percentage: \(statistics)") diff --git a/Toki Trainer/Views/FlashCardView.swift b/Toki Trainer/Views/FlashCardView.swift index 87b7976..27c889a 100644 --- a/Toki Trainer/Views/FlashCardView.swift +++ b/Toki Trainer/Views/FlashCardView.swift @@ -39,7 +39,7 @@ extension Binding { struct FlashCardStack: View { @Environment(\.managedObjectContext) private var viewContext - @FetchRequest(entity:FlashCardAnswer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \FlashCardAnswer.word, ascending: false)]) var flashCardAnswers: FetchedResults + @FetchRequest(fetchRequest: K.getFlashCardAnswersFetchRequest) var flashCardAnswers: FetchedResults var dictionary: [TokiDictEntry] @State private var flashCards: [FlashCard] = []