TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Checkpoint 5 - filter method

Forums > 100 Days of SwiftUI

Hello everyone. I'm new to SwiftUI and still learning the jargon too. I'm struggling a bit with the filter method as in what to put for the criteria to filter. Is there a reference to how filter will accept criteria? My thought process is that if isMultipleOf 2 the code would return false and then filter those numbers out since we're looking for odd numbers. I just can't wrap my head around what to write. Could be a total brain fart on my part too. I made this checkpoint work a different way than using filter, but still filtered the numbers with if and else statements. I hope that makes sense. Have a fabulous day whomever reads this!

   

A filter is a question that you ask each and every object in an array. If the object answers true to the question, it is copied to a new array!

Try this example in Playgrounds for fun.

print("Run this code in playgrounds.")
print("Add a few more 🧙🏾🪄🧙🏻🪄, and add more filters.")
print("=============================================")

struct Wizard {
    let name:  String   // each student has a name
    let house: House    // each student belongs to a school house

    // A wizard can only belong to one of these four houses
    enum House {
        case hufflepuff, griffendor, slytherin, ravenclaw
    }

    // Sample selection of wizards
    static var wizards = [
        Wizard(name: "Harry Potter",     house: .griffendor ),
        Wizard(name: "Luna Lovegood",    house: .ravenclaw  ),
        Wizard(name: "Draco Malfoy",     house: .slytherin  ),
        Wizard(name: "Albus Dumbledore", house: .ravenclaw  ),
        Wizard(name: "Ron Weasley",      house: .griffendor ),
        Wizard(name: "Delores Umbridge", house: .hufflepuff ),
        Wizard(name: "Moaning Myrtle",   house: .ravenclaw  ),
        Wizard(name: "Hermione Granger", house: .griffendor )
    ]
}

// Imagine asking all the students to stand in a line.
// Then you walk down the line asking each student, "Are you in House Griffendor?"
// If they answer "Yes", you add them to the griffendorWizards roster.
let griffendorWizards = Wizard.wizards.filter { $0.house == .griffendor }

// for each wizard in griffendor array, print their name
griffendorWizards.forEach { print($0.name) }

print("\(Wizard.House.griffendor) has \(griffendorWizards.count) students.")

Keep Coding!

You can create your own rules. It's up to your imagination, or your application's use case. Please return here and let us know if you understand filters a bit better.

1      

Thank you so much that was fun and educational! :)

   

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.