Search Your Question

What is NSPredicate?

Ans : Using NSPredicate, we can filter records before fetching from Coredata or collections. It work in place of where clause in sql.

NSPredicate on Core Data :

When we want data from coredata of only who's name is Manan Shah. So using predicate we can filter as following :

var strName = "Manan Shah"
request.predicate = NSPredicate(format: "name = %@", strName)

NSPredicate on Collections : 

let manan = Person(firstName: "Manan", lastName: "Shah", age: 28)
let sagar = Person(firstName: "Sagar", lastName: "Shah", age: 26)

let people = [manan, sagar] as NSArray

let thirtiesPredicate = NSPredicate(format: "age >= 27")

people.filtered(using: thirtiesPredicate)
Result : manan object will be return.

No comments:

Post a Comment