UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

3      

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!

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.