Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Array in Swift based on multiple properties

Tags:

arrays

swift

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”)
})
like image 284
Paragon Avatar asked Oct 20 '25 15:10

Paragon


1 Answers

let filteredArray = users.filter({ $0.firstName.lowercased().contains("firstName") || $0.lastName.lowercased().contains("lastName") || ... })
like image 65
flowGlen Avatar answered Oct 23 '25 05:10

flowGlen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!