Hey there!
So I am not entirely certain what the first challenge for project 12 is asking--mostly because I don't understand how it differs beyond the current code, and furthermore, why it would be necessary.
Any clarification would be great! Thanks in advance!
I've included the code and challenge details for your convenience.
Project 12: Challenge 1
Make FilteredList() accept a string parameter that controls which predicate is applied. You can use Swift’s string interpolation to place this in the predicate.
FilteredList() Code
struct FilteredList<T: NSManagedObject, Content: View>: View {
@FetchRequest var fetchRequest: FetchedResults<T>
let content: (T) -> Content
var body: some View {
List(fetchRequest, id: \.self) { item in
self.content(item)
}
}
init(filterKey: String, filterValue: String, @ViewBuilder content: @escaping (T) -> Content) {
_fetchRequest = FetchRequest<T>(sortDescriptors: [], predicate: NSPredicate(format: "%K BEGINSWITH %@", filterKey, filterValue))
self.content = content
}
}
How it's used in ContentView
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
@State private var lastNameFilter = "A"
var body: some View {
VStack {
FilteredList(filterKey: "lastName", filterValue: lastNameFilter) { (singer: Singer) in
Text("\(singer.wrappedFirstName) \(singer.wrappedLastName)")
}
Button("Show A") {
lastNameFilter = "A"
}
Button("Show S") {
lastNameFilter = "S"
}
}
}