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

Filter ForEach From a Binding

Forums > Swift

I have follow this tutorial, but I can't filter array anymore if I use binding.

Im getting an error

Generic struct 'ForEach' requires that 'Binding<[Contact]>.Element' (aka 'Binding<Contact>') conform to 'Hashable'

If I remove $ binding, Im able to do filtering again.

This is my class

class ContactsViewModel: ObservableObject {

    @Published var contactsList: [Contact] = []
    @Published var favoritesList: [Contact] = []


Here's a short bit I wrote in another forum post about ForEach: Notes on ForEach

TLdr: ForEach is a view factory. It makes a number of views from a collection of things.

If you have a collection of book objects, ForEach will loop through each book object in your collection and make a BookView for each of those individual objects.

The error message gives you a clue:

Generic struct 'ForEach' requires that 'Binding<[Contact]>.Element' (aka 'Binding<Contact>') conform to 'Hashable'

What is the Contact object you're asking ForEach to process? You do not show this object in your code snip. XCode is complaining that you're not providing a collection of Contact objects. If you are, your Contact objects are not Hashable.

@rooster wrote a nice piece on Identifiable and Hashable. Read it here: All about Hashable


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.