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

Filter ForEach From a Binding

Forums > Swift

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

https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-a-list-or-a-foreach-from-a-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] = []
 }

3      

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

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.