hellosheikh hellosheikh - 2 years ago 245
Swift Question

NSPredicate Exact Match with String

hello I am working on swift. I need to know how can I find results which matches the exact string. Here's my code

let userID: String = String(sender.tag)
// Create a Predicate with mapping to trip_id
let filterByRequest: NSPredicate = NSPredicate(format: "%K.%K CONTAINS[c] %@", "ProductRequest", "user_id", userID)
// Filter your main array with predicate, resulting array will have filtered objects
let filteredArray: [AnyObject] = self.array.filteredArrayUsingPredicate(filterByRequest)

The problem is If user id is 69 it shows results of users whose id is 69, 6, and 9.

I googled but I find some answers closed to my question but they were all in objective C.

Answer Source

Use MATCHES in predicate as following :

let filterByRequest: NSPredicate = NSPredicate(format: "%K.%K MATCHES %@", "ProductRequest", "user_id", userID)

Hope it helps..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download