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

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

   

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.