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

Day 59 - Challenge 3

Forums > 100 Days of SwiftUI

@SD449  

Hi

Can someone point me in the right direction on how to tackle Challenge 3 on Day 59; Modify the predicate string parameter to be an enum such as .beginsWith, then make that enum get resolved to a string inside the initializer.

So far all I have is;

    enum predicateParameter {
        case beginsWith
    }

I tried inserting this into the init and then tried adding a function and adding that to the init but neither worked.

To be honest I am not sure I really understand the premise of the challenge, hence a pointer to get me started would be much appreciated.

Thank You

3      

I was also struggling with the premise but in order to clarify the purpose of the challenge I simplified it for myself a lil bit. Instead of an enum, I made an array with the predicate parameters as so:

let parameters = ["BEGINSWITH", "ENDSWITH", "CONTAINS[c]"]

Then I edited the predicate in the FilteredList initializer to this, where 'psp' was an Int selected by a Picker in ContentView:

NSPredicate(format: "%K \(parameters[psp]) %@", filterKey, filterValue)

This allowed me to sort the names based on the aforementioned parameters <3

3      

You have to use raw values!

enum PredicateType: String {
    case beginsWith = "BEGINSWITH"
    case contains = "CONTAINS"
    case containsCI = "CONTAINS[c]"
}

Then you can use the string values in this way

PredicateType.beginsWith.rawValue

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.