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

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! :)

   

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.