WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Project 12: Challenge 1 clarification

Forums > 100 Days of SwiftUI

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"
            }
        }
    }

1      

Your predicate is hard wired for ONE filter, namely:

NSPredicate(format: "%K BEGINSWITH %@", filterKey, filterValue))  // look for something that begins with something

You only look for singers whose last name begins with a filterValue But what if you wanted three predicates?

  1. Find singers whose last name starts with an "S"
  2. Find lastnames where lastname has more than 6 letters.
  3. Find lastnames that end with "T"

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.

In each button's code, you might try to form a different predicate for each filter. Then you'll need to pass that into the FilteredList() function. In that function, @twostraws gives you a hint about string interpolation.

Good luck! Report back and post your updated code!

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.