Filtering an NSArray with NSPredicate - iphone

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];

Related

How do I Add a searchbar with NSPredicate

I am getting an error when add the
-(void) statement.
"use of undeclared identifier "filterContentForSearchText"
//start of Predicate
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF contains[cd] %#",
searchText];
searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}
end of search
Have a look at this sample, made using NSPredicate if your array doesn't contain this kind of data, show us your data or array handling, which type of data you are having in your array.

NSFetchRequest not working with predicate

I have this code:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Entry"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"version == %#", #"1.0"];
[request setPredicate:predicate];
NSArray *results = [managedObjectContext executeFetchRequest:request error:nil];
The entity "Entry" has an attribute called "version", which is a string. The predicate above doesn't seem to only be returning entries with the string as "1.0" though, returning some entries which are set to "1.1".
Am I doing this wrong?
Try putting round braces () around your predicate expression
i.e.
"(version == %#)"
have a look at the apple doc's
and try it with like in case of == or:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(version == %#)", #"1.0"];

Correct way to create a NSPredicate

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

NSPredicate filter

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];

Setting predicate to search in Core Data

I need to perform search Core Data entities in my app for iPhone.
In my Core Data I have an entity 'myEntity', that has two properties 'stringOne' and 'stringTwo'; both defined as NSString.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"stringOne contains[cd] %#", self.keyword4Search];
[fetchRequest setPredicate:predicate];
My code above only search entities that contains keyword4Search in stringOne only.
What I need is to search entities that contains keyword4Search in [stringOne OR stringTwo].
I expect kind of
predicateWithFormat:#"stringOne or stringTwo contains[cd] %#", self.keyword4Search];
where should I look?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"stringOne contains[cd] %# OR stringTwo contains[cd] %#", self.keyword4Search, self.keyword4Search];