predicate to compare Int32

310 views Asked by At

extension Element {

@nonobjc public class func fetchRequest() -> NSFetchRequest<Element> {
    return NSFetchRequest<Element>(entityName: "Element");
}

@NSManaged public var elementId: Int32
@NSManaged public var elementName: String?
@NSManaged public var elementText: String?
@NSManaged public var pageId: Int32
@NSManaged public var sortOrder: Int32

}

Here i want to compare the pageId using NSPredicate. How can i do it? I tried the following.

Case 1:

elementFetch.predicate = NSPredicate(format: "pageId == %@","1")

var Data = try moc.fetch(elementFetch as! NSFetchRequest<NSFetchRequestResult>) as! [Element]

Case 2:

elementFetch.predicate = NSPredicate(format: "pageId == %i",1)
var Data = try moc.fetch(elementFetch as! NSFetchRequest<NSFetchRequestResult>) as! [Element]

Case 3:

elementFetch.predicate = NSPredicate(format: "pageId == %d",1)
var Data = try moc.fetch(elementFetch as! NSFetchRequest<NSFetchRequestResult>) as! [Element]

None of the case works. I am not able to find any documentation on this predicates especially to compare INT32

Please help

1

There are 1 answers

0
vadian On

A literal value is pretty simple

elementFetch.predicate = NSPredicate(format: "pageId == 1")