Last active
April 22, 2021 20:32
-
-
Save carlynorama/a0d45aeb8f63c424671a4a5ed29e0fea to your computer and use it in GitHub Desktop.
HWS Day 59: Core Data wrap up... ERROR @State private var filterStyleEnum = FilteredListBonusRoundView.FilterType.beginsWith how to fix?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FilteredListBonusRoundView.swift | |
// HWS100CoreDataEx | |
// | |
// Created on 4/22/21. | |
// https://www.hackingwithswift.com/books/ios-swiftui/core-data-wrap-up | |
import SwiftUI | |
import CoreData | |
struct FilteredListBonusRoundView<T: NSManagedObject, Content: View>: View { | |
var fetchRequest: FetchRequest<T> | |
var results: FetchedResults<T> { fetchRequest.wrappedValue } | |
let content: (T) -> Content | |
var body: some View { | |
List(results, id: \.self) { result in | |
self.content(result) | |
} | |
} | |
init(filterKey: String, filterValue: String, sortInstructions: [NSSortDescriptor], @ViewBuilder content: @escaping (T) -> Content) { | |
fetchRequest = FetchRequest<T>(entity: T.entity(), sortDescriptors: sortInstructions, predicate: NSPredicate(format: "%K BEGINSWITH %@", filterKey, filterValue)) | |
self.content = content | |
} | |
init(filterKey: String, filterValue: String, filterType:String, sortInstructions: [NSSortDescriptor], @ViewBuilder content: @escaping (T) -> Content) { | |
fetchRequest = FetchRequest<T>(entity: T.entity(), sortDescriptors: sortInstructions, predicate: NSPredicate(format: "%K \(filterType) %@", filterKey, filterValue)) | |
self.content = content | |
} | |
init(filterKey: String, filterValue: String, filterType:FilterType, sortInstructions: [NSSortDescriptor], @ViewBuilder content: @escaping (T) -> Content) { | |
fetchRequest = FetchRequest<T>(entity: T.entity(), sortDescriptors: sortInstructions, predicate: NSPredicate(format: "%K \(filterType.rawValue) %@", filterKey, filterValue)) | |
self.content = content | |
} | |
init(sortInstructions: [NSSortDescriptor], filter:NSPredicate?, @ViewBuilder content: @escaping (T) -> Content) { | |
fetchRequest = FetchRequest<T>(entity: T.entity(), sortDescriptors: sortInstructions, predicate: filter) | |
self.content = content | |
} | |
} | |
//moved the enum into an extension in the off chance that made a difference. It did not. | |
extension FilteredListBonusRoundView { | |
public enum FilterType:String { | |
case beginsWith = "BEGINSWITH" | |
case like = "LIKE" | |
case matches = "MATCHES" | |
case endsWith = "ENDSWITH" | |
case contains = "CONTAINS" | |
} | |
} | |
//struct FilteredListBonusRoundView_Previews: PreviewProvider { | |
// static var previews: some View { | |
// FilteredListBonusRoundView() | |
// } | |
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SingersBonusRoundView.swift | |
// HWS100CoreDataEx | |
// | |
// Created on 4/22/21. | |
// https://www.hackingwithswift.com/books/ios-swiftui/core-data-wrap-up | |
import SwiftUI | |
struct SingersBonusRoundView: View { | |
@Environment(\.managedObjectContext) var moc | |
@State private var lastNameFilter = "A" | |
@State private var filterStyle = "BEGINSWITH" | |
// @State private var filterStyleEnum:FilteredListBonusRoundView.FilterType = .beginsWith | |
@State private var filterStyleEnum = FilteredListBonusRoundView.FilterType.beginsWith | |
@State private var reusablePredicate:NSPredicate? = nil | |
let sortInstructions = [NSSortDescriptor(keyPath: \Singer.lastname, ascending: true), NSSortDescriptor(keyPath: \Singer.firstname, ascending: true)] | |
var body: some View { | |
VStack { | |
// list of matching singers | |
//SingerFilteredListView(filter: lastNameFilter) | |
// FilteredListBonusRoundView(filterKey: "lastname", filterValue: lastNameFilter, sortInstructions: [NSSortDescriptor(keyPath: \Singer.lastname, ascending: true), NSSortDescriptor(keyPath: \Singer.firstname, ascending: true)]) { (singer: Singer) in | |
//FilteredListBonusRoundView(sortInstructions: sortInstructions, filter: reusablePredicate) { (singer: Singer) in | |
FilteredListBonusRoundView(filterKey: "lastname", filterValue: lastNameFilter, filterType: .beginsWith, sortInstructions: [NSSortDescriptor(keyPath: \Singer.lastname, ascending: true), NSSortDescriptor(keyPath: \Singer.firstname, ascending: true)]) { (singer: Singer) in | |
Text("\(singer.wrappedFirstName) \(singer.wrappedLastName)") | |
} | |
Button("Add Examples") { | |
let taylor = Singer(context: self.moc) | |
taylor.firstname = "Taylor" | |
taylor.lastname = "Swift" | |
let ed = Singer(context: self.moc) | |
ed.firstname = "Ed" | |
ed.lastname = "Sheeran" | |
let adele = Singer(context: self.moc) | |
adele.firstname = "Adele" | |
adele.lastname = "Adkins" | |
try? self.moc.save() | |
} | |
Button("Show A") { | |
//reusablePredicate = NSPredicate(format: "%K BEGINSWITH %@", "lastname", "A") | |
lastNameFilter = "A" | |
filterStyle = "BEGINSWITH" | |
} | |
Button("Show S") { | |
//reusablePredicate = NSPredicate(format: "%K BEGINSWITH %@", "lastname", "S") | |
lastNameFilter = "S" | |
filterStyle = "BEGINSWITH" | |
} | |
Button("Show All") { | |
//reusablePredicate = nil | |
lastNameFilter = "*" | |
filterStyle = "LIKE" | |
} | |
} | |
} | |
} | |
struct SingersBonusRoundView_Previews: PreviewProvider { | |
static var previews: some View { | |
SingersBonusRoundView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment