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
126
votes
9 answers

NSPredicate: filtering objects by day of NSDate property

I have a Core Data model with an NSDate property. I want to filter the database by day. I assume the solution will involve an NSPredicate, but I'm not sure how to put it all together. I know how to compare the day of two NSDates using…
98
votes
2 answers

NSArray with NSPredicate using NOT IN

I have an NSArray that I want to filter out certain objects using an NSPredicate, I was hoping I could use NOT IN since I saw that I can easily do an IN. So I have my array: self.categoriesList Then I get the values I want to remove: NSArray…
Slee
  • 27,498
  • 52
  • 145
  • 243
95
votes
6 answers

Using NSPredicate to filter an NSArray based on NSDictionary keys

I have an array of dictionaries. I want to filter the array based on a key. I tried this: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SPORT == %@)", @"Football"]; NSArray *filteredArray = [data…
Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
77
votes
4 answers

NSPredicate to test for NULL, and blank strings

I have an NSArray and need to filter out any strings that are null or rather, have ' ' (empty string). How do I do that? I have tried doing: NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"(name!=nil)"]; but that doesn't seem to…
Doz
  • 7,009
  • 12
  • 61
  • 69
76
votes
20 answers

How to fetch all contacts record in iOS 9 using Contacts Framework

Most part of AddressBook framework is deprecated in iOS 9. In the new Contacts Framework documentation only shows how to fetch records matches a NSPredicate, but what if I want all the record?
Jay Hu
  • 6,041
  • 3
  • 22
  • 33
70
votes
3 answers

Core Data: NSPredicate for many-to-many relationship. ("to-many key not allowed here")

I have two entities named "Category" and "Article" which have a many to many relationship. I want to form a predicate which searches for all articles where category.name is equal to some value. I have the following: NSEntityDescription …
Oh Danny Boy
  • 4,857
  • 8
  • 56
  • 88
58
votes
4 answers

How do I set up a NSPredicate to look for objects that have a nil attribute

I have a ManagedObject class, and one of the members of the class is a NSDate. I would like to display all objects of the class for which the date is NOT set. I tried using a predicate like this: NSPredicate *predicate = [NSPredicate…
mezulu
  • 1,155
  • 1
  • 12
  • 22
58
votes
3 answers

NSPredicate that is the equivalent of SQL's LIKE

I'm looking for a way to use NSPredicate to set a LIKE condition to fetch objects. In addition to that, an OR would be useful as well. I'm trying to do something where if a user searches "James" I can write an NSPredicate that will do the equivalent…
randombits
  • 47,058
  • 76
  • 251
  • 433
56
votes
2 answers

iPhone - getting unique values from NSArray object

I have an NSArray formed with objects of a custom class. The class has 3 (city, state, zip) string properties. I would like to get all unique state values from the array. I did read through the NSPredicate class but couldn't make much of how to use…
lostInTransit
  • 70,519
  • 61
  • 198
  • 274
55
votes
4 answers

Combining 'AND' and 'OR' Condition in NSPredicate

Back again needing more help with constructing my NSPredicates :( Category { name:string subs<-->>SubCategory } SubCategory { name:string numbervalue:NSNumber } I would like to know how to create a predicate with AND and OR. For…
dubbeat
  • 7,706
  • 18
  • 70
  • 122
53
votes
4 answers

Core Data primary key ID for a row in the database

Suppose I have a list of books stored in Core Data. I want to search for a book by it's primary key ID. I know the sqlite file created by Core Data has an ID column in each table, but this doesn't seem to be exposed to me in anyway. Does anyone have…
WoodenKitty
  • 6,521
  • 8
  • 53
  • 73
53
votes
4 answers

How to search an NSSet or NSArray for an object which has an specific value for an specific property?

How to search an NSSet or NSArray for an object which has an specific value for an specific property? Example: I have an NSSet with 20 objects, and every object has an type property. I want to get the first object which has [theObject.type…
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
51
votes
3 answers

case insensitive NSPredicate with single result in CoreData

Here is my current NSPredicate: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UPC==%@ OR ItemID==%@", aUPCCode,aUPCCode]; How can I make this case insensitive? And I do not want to do any partial matching. Example if they enter…
Slee
  • 27,498
  • 52
  • 145
  • 243
46
votes
1 answer

NSPredicate query for not containing a specific string

Looked high and low for this one but can't find my answer. I am looking to query core data for all records which are NOT equal to a specified string. For example, all records which are not equal to the current session ID. I have tried these to no…
sangony
  • 11,636
  • 4
  • 39
  • 55
44
votes
2 answers

NSPredicate compare with Integer

How can I compare two NSNumbers or a NSNumber and an Integer in an NSPredicate? I tried: NSNumber *stdUserNumber = [NSNumber numberWithInteger:[[NSUserDefaults standardUserDefaults] integerForKey:@"standardUser"]]; fetchRequest.predicate =…
wolfrevo
  • 2,006
  • 2
  • 25
  • 32
1
2 3
99 100