Search code examples
swiftnsarraynspredicate

Array filter with aggregate operators


array.filtered does not exist in Swift anymore, I am looking for a way to use 'IN' operator in array.filter closure same as how I used it with NSPredicate.

Before the query was something like

let predicate = NSPredicate(format: "(user in %@)", overallUsers)
tasksArray.filtered(using: predicate)

Now this cannot be used with swift4, so how can I achieve the same with array.filter{} ? I tried something like this

tasksArray.filter{$0.user in overallUsers}

This fails with error 'Cannot convert value of type '@lvalue User' to closure result type 'Bool''. How this should be actually done ?


Solution

  • You can check it like this.

    taskArray.filter( { overallUsers.contains($0.user) } )