struct User{
var firstName:String
var lastName:String
var city:String
var email:String
}
var users = [User]
I am trying to filter users as someone is typing in the textfield. Its like Search for the email. It should show all the matching results but shouldn’t duplicate the same user. I am able to filter the array based on one property such as name but not sure how to filter the array based on all the properties.
I’ve implemented the UITextField delegate and have this code for filtering.
let filteredArray = users.filter({ (user) -> Bool in
return user.firstName.lowercased().contains(“John”)
})
filter()closure. You iterate the array, the current element is named in your caseuser(that's the param). Then, according touservalues, you decide to returntrueif you want to keep it, andfalseif you don't. Currently, you return true if thefirstName.lowercased()contains "John". So check on the other property using an OR between them?||) if you want to increase the number of matches. Use and (&&) if you only want to match items that meet ALL the criteria.