User User - 1 year ago 174
Swift Question

How to filter NSArray in Swift?

self.filteredUserData = self.userData.filter({( listing: NSDictionary) -> Bool in
let stringMatch = listing["name"].rangeOfString(searchText)
return stringMatch != nil

I was going to do something like above, but I realized
'NSArray' does not have a member named 'filter'
and not a Swift Array. How can I filter NSArrays in Swift?

Answer Source

Here's what I ended up doing:

let resultPredicate = NSPredicate(format: "name contains[c] %@", searchText)
self.filteredUserData = self.userData.filteredArrayUsingPredicate(resultPredicate)

Objective-C version:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
self.filteredUserData = [self.userData filteredArrayUsingPredicate:resultPredicate];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download