I have an NSArray that I would like to filter with a predicate. Here is what I have.
NSLog(#"text is %#",txtSearch.text);
NSPredicate *bPredicate =
[NSPredicate predicateWithFormat:#"SELF beginswith[c] '%#'",txtSearch.text];
arrSearchedPlayers =
[arrPlayers filteredArrayUsingPredicate:bPredicate];
NSLog(#"array after searched is %#",arrSearchedPlayers);
When I enter a text my array keeps empty. But when I try this predicate,
NSPredicate *bPredicate =
[NSPredicate predicateWithFormat:#"SELF beginswith[c] 's'"];
I got the right results.Anybody has an idea what the problem is?
Kind regards
Remove extra quotes in predicate
[NSPredicate predicateWithFormat:#"SELF beginswith[c] %#",txtSearch.text];
i found 2 different ways to create a NSPredicate.
Way 1:
NSExpression *exprName = [NSExpression expressionForKeyPath:#"name"];
NSExpression *exprFilter = [NSExpression expressionForConstantValue: name ];
NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression: exprName
rightExpression: exprFilter
modifier: NSDirectPredicateModifier
type: NSContainsPredicateOperatorType
options: NSCaseInsensitivePredicateOption];
Way 2:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"name CONTAINS[c] %#", name];
What is the best way to create a NSPredicate and also prevent SQLInjection?
The first way is useful if you are setting up a complex predicate programmatically. Other than that, the second way is fine. You don't have to worry about SQL injection with Core Data.
relevant
also relevant
I have never used NSPredicate before so please bear with me. I have an array which have a boolean key as "isChecked". I want to filter the array which have the boolean set as "YES", any idea how can I do it, plus if there is any reference to such query methods that would be nice to have it handy.
Thanks
//NSArray * myArray is your array
//containing your objects that each have an isChecked property
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"isChecked == YES"];
NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate];
// filteredArray is the new array that only contains your checked items
Cheers
First you create an NSPredicate instance. In this case, we'll use -initWithFormat:.
NSPredicate *predicate = [[NSPredicate alloc] initWithFormat:#"isChecked == YES"];
Then we'll use the -filteredArrayUsingPredicate: method on NSArray to get an NSArray of all the objects which match the predicate.
NSArray *filteredArray = [originalArray filteredArrayUsingPredicate:predicate];
I'm trying to setup a NSPredicate to filter data.
I have a property of an entity called code that is in format like 55.534.
I'm trying to fetch all data in a range, for example 50-60.
I have this but it doesn't work.
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"ANY code BETWEEN %#", [NSArray arrayWithObjects:self.predicateFilterStart, self.predicateFilterEnd, nil]];
First of all, your SQL syntax is wrong. Your predicate format should be
ANY code BETWEEN x AND y
Secondly, %# formatter should receive NSString, while you're passing NSArray.
Try this:
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"ANY code BETWEEN %# AND %#", self.predicateFilterStart, self.predicateFilterEnd];
I am currently having an issue pulling all data from db whereby i.e 1 parameter is TRUE.
I am using NSPredicate and below is a sample code
NSManagedObjectContext *context = managedObjectContext_;
if (!context) {
// Handle the error.
NSLog(#"ERROR CONTEXT IS NIL");
}
NSEntityDescription *entity = [NSEntityDescription entityForName:#"tblcontent" inManagedObjectContext:managedObjectContext_];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"bookmarked == YES"];
[request setPredicate:predicate];
I tried setting predicatewithformat to almost everything but it still does not pull out bookmarks which have a YES value.
I even tried (#"bookmarked == %d",YES) but with not luck. I don't want to have to get the whole array and then filter it manually by doing if(object.bookmarked == YES) .....blabla.
I will really appreciate some help.
Many thanks.
Based on Apple Document Here, we can use the following two methods to compare Boolean:
NSPredicate *newPredicate = [NSPredicate predicateWithFormat:#"anAttribute == %#",[NSNumber numberWithBool:aBool]];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"anAttribute == YES"];
However, the above predicate cannot get out the ones with empty anAttribute. To deal with an empty attribute, you need the following method according to Apple document here:
predicate = [NSPredicate predicateWithFormat:#"firstName = nil"]; // it's in the document
or
predicate = [NSPredicate predicateWithFormat:#"firstName == nil"]; // == and = are interchangeable here
Sneaking in with the Swift 3/4 answer:
let predicate = NSPredicate(format: "boolAttribute == %#", NSNumber(value: true))
We have to use NSNumber apparently because a literal bool is not acceptable per Apple.
Stolen from here ;)
For some reason, Flow's solution would not work for me:
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"anAttribute == YES"];
However, this did:
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"anAttribute == 1"];
I'm late to the party and as discussed using 0 and 1's is the way to go however there is a better way to show this by using NSNumber BOOL literals like #YES or #NO. It converts it to a 1 or a 0 but is more visually friendly.
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:#"anAttribute == %#", #NO];
Core Data entities does not have any default values when you create attributes for Entity, so to make your predicate work you should either set default values for boolean attributes or use predicate in this way.
If you provide default value (NO or YES) for any boolean property of Entity then use predicate like this
[NSPredicate predicateWithFormat:#"boolAttribute == %#", #NO];
[NSPredicate predicateWithFormat:#"boolAttribute == NO", #NO];
[NSPredicate predicateWithFormat:#"boolAttribute == %0"];
If you do not have default values or some of Entities was already created without default values then to filter by false value use this sentence:
[NSPredicate predicateWithFormat:#"boolAttribute == %# || boolAttribute == nil", #NO];
Swift:
fetchRequest.predicate = NSPredicate(format: "%K == NO",#keyPath(MyEntity.isOccupied))
or
fetchRequest.predicate = NSPredicate(format: "%K == NO",\MyEntity.isOccupied)
You haven't mentioned what results you're getting. Two things that are missing from your code listing is where you set your request's entity and where you actually ask the context to execute the fetch request. I'd start there.
For me, it happened coz of assign value in Object is wrong when saving.
You have to save like this
YourNSMNanagedObject.visibleBoolean = [[NSNumber alloc] initWithBool:false]
Then all predicate will work when fetching.
E.g.
// Filter only unvisible item
NSPredicate *favouriteFilter = [NSPredicate predicateWithFormat:#"visibleBoolean==NO"];
I keep getting bit by this one and it's not always clear that booleans in core data get saved as NSNumbers.
For the most part I make sure when creating entities I set #NO for any Boolean properties but there's always the case where I spend a whole bunch of time trying to figure out why a test doesn't pass when I've forgot to set #NO on entity creation.
This might not be the best choice for clear code but I've now started to always use != predicates for booleans in core data like (isCompleted != YES) instead of (isCompleted == NO). The fact nil == false pattern is true everywhere else but not in core data predicates and properties can be very confusing.