User User - 10 months ago 137
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?


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];