3
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?

User
  • 23,729
  • 38
  • 124
  • 207
  • possible duplicate of [How to filter array with objects by using predicate?](http://stackoverflow.com/questions/18714634/how-to-filter-array-with-objects-by-using-predicate) – matt Sep 21 '14 at 02:49
  • 2
    lol this is not an objective-c question. completely different syntax. – User Sep 21 '14 at 02:51
  • 1
    You're not listening. Your question is not a _language_ question. It's a Cocoa / Foundation question (that's what NSArray is). You need to be language-agnostic if you want to work with Cocoa methods. The "translation" from that answer to yours is trivial; a machine could do it. – matt Sep 21 '14 at 03:17

1 Answers1

5

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];
Craig McMahon
  • 1,550
  • 1
  • 14
  • 36
User
  • 23,729
  • 38
  • 124
  • 207