Questions tagged [nspredicate]

The NSPredicate class is used in Mac OS X and iOS development to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.

The NSPredicate class is used in Mac OS X and iOS development to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.

An example predicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(lastName like[cd] %@) AND (birthday > %@)", lastNameSearchString, birthdaySearchDate];

Multiple predicates can be combined into a more complex predicate with NSCompoundPredicate.

Full reference available in Apple's docs for NSPredicate. Also refer to the Predicate Programming Guide.

2953 questions
28
votes
2 answers

How to limit the result count of NSFetchRequest?

I want to have a feature of "Recent 20 Items" in my iOS app. I use Core Data and NSFetchRequest. How can I limit the result number to 20 to achieve this? Thank you in advance! Kai.
nonamelive
  • 6,510
  • 8
  • 40
  • 47
28
votes
1 answer

EXC_BAD_ACCESS when building nspredicate

I am calculating the number of months between a birthdate and today. With that number, I am building a predicate to fetch objects from core data. Although the number of months is calculated correctly (as the log shows), I am getting a EXC_BAD_ACCESS…
mrd
  • 4,561
  • 10
  • 54
  • 92
27
votes
2 answers

NSFetchedResultsController with predicate ignores changes merged from different NSManagedObjectContext

I am presenting table view contents using NSFetchedResultsController which has a predicate: [NSPredicate predicateWithFormat:@"visible == %@", [NSNumber numberWithBool:YES]] On background thread using separate NSManagedObjectContext I update few of…
ampatspell
  • 844
  • 7
  • 10
27
votes
2 answers

Swift NSPredicate throwing EXC_BAD_ACCESS(Code=1, address=0x1) when compounding statements

I am trying to use NSPredicate in Swift to query Core Data but it throws an EXC_BAD_ACCESS(Code=1, address=0x1) error when trying to run it, what am I doing wrong? Here is the file where the error happens class LevelsScreenModel : UIViewController…
Cesarg2199
  • 569
  • 1
  • 6
  • 18
27
votes
3 answers

Is it possible to filter an NSArray by class?

Is there a way construct a predicate to filter by class type? I currently loop through the array and check to the class of each object. Maybe there is a cleaner way?
Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
27
votes
2 answers

Can I use an NSPredicate in Swift with a nil argument?

I'm trying to convert a project that uses Core Data from Objective-C to Swift. The data model is structured so that I have one master folder which contains other folders - and those folders can also contain other folders, via a "parentFolder"…
Jim Rhoades
  • 3,310
  • 3
  • 34
  • 50
26
votes
3 answers

NSPredicate - Unable to parse the format string

I am trying to write a NSPredicate to fetch rows with my_column value with this string "193e00a75148b4006a451452c618ccec" and I get the below crash. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse…
Satheesh
  • 10,998
  • 6
  • 50
  • 93
26
votes
2 answers

NSPredicate Exact Match with String

I have a NSPredicate like this: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name CONTAINS %@", myString]; But that will return anything which contains that string. For example: If my entity.name's…
CoreCode
  • 2,227
  • 4
  • 22
  • 24
25
votes
1 answer

NSPredicate for searching within relationship entities

I have an entity called Band which has a to-many relationship to a Category entity. The Category entity just contains a categoryName string attribute. An example record: Band: bandName: Kiss bandCategories: - >…
cannyboy
  • 24,180
  • 40
  • 146
  • 252
25
votes
3 answers

NSPredicate with Multiple parameters

Hello Need help with predicate. The problem is that I want to have one function which will fetch from database depends on the multiple values it receives, but the values don't always exist, does it mean I have to create several different…
Rouslan Karimov
  • 585
  • 2
  • 7
  • 19
25
votes
1 answer

Filter Core Data results by property IN array

I currently have Core Data successfully returning all of the results for a specific entity titled Event: NSManagedObjectContext *context = [delegate managedObjectContext]; NSEntityDescription *entityDescription = [NSEntityDescription…
markdorison
  • 139,374
  • 27
  • 55
  • 71
25
votes
5 answers

NSPredicate: Fetch one of each kind

I want to create an NSFetchRequest for objects like this: The Object is Car which has an attribute color. I have four cars: car1.color = red car2.color = red car3.color = blue car4.color = green I want to create an NSPredicate that selects only one…
arnoapp
  • 2,416
  • 4
  • 38
  • 70
25
votes
2 answers

Filter Array with dictionaries using NSPredicate

There is an Array with each element being a NSDictionary. NSMutableArray *mutArr = [NSMutableArray array]; for (Person *person in persons) { NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:person.name, @"name", person.email,…
lu yuan
  • 7,207
  • 9
  • 44
  • 78
23
votes
4 answers

How to use binary flags in Core Data?

I have an int32 attribute in a Core Data database. I use this int as an enum bit field. Is it possible to create a NSPredicate to query items based on the binary value of this int ? Something like @"bitFieldAttribute & 0x0001"? I'm also wondering if…
CodeFlakes
  • 3,671
  • 3
  • 25
  • 28
23
votes
2 answers

How can I make a CKQuery without predicate?

I need to query all the record types, without any filtering. I tried two ways: let query = CKQuery(recordType: "Pm", predicate: nil) let query = CKQuery(recordType: "Pm", predicate: NSPredicate(format: "")) I got…
János
  • 32,867
  • 38
  • 193
  • 353