Setting predicate to search in Core Data - iphone

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

Related

Filtering an NSArray with NSPredicate

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

NSPredicate using OR error

First time building an NSPredicate.
I would like to search a managedobjectcontext using this logic:
Search for a, grab all matches
Search for b, grab all matches, etc....
Nsarray *results = (has all a results, b results, etc);
My attempted predicate is:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"name== %# OR name == %# OR name == %#",a,b,c];
However I get errors with this predicate...
Edited: Sample method I wrote
-(NSPredicate*)parsePartsIntoAPredicate:(NSMutableArray*)inputPartsNames{
NSSet *keys=[NSSet setWithArray:inputPartsNames];
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"any self.#name in %#", keys];
NSLog(#"predicate = %#",predicate);
return predicate;
}
Clarify: I have a database of cars (20,000) Each car has multiple parts. I want to find all cars that have part a, and all cars that have part b, and all that have part c. Then I want to return an array with cars with part a, b, c, etc...
If you think there is a better way let me know, but I am approaching this backwards. I am saying
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Cars" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:[self parsePartsIntoAPredicate:inputParts]];
NSError *error;
NSArray *records = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
What am I doing wrong?
NSString *key;
NSMutableArray *tempArray;
NSPredicate *searchForName = [NSPredicate predicateWithFormat:#"name = %#", key];
NSArray *filterArray = [tempArray filteredArrayUsingPredicate:searchForName];
NSLog(#"%#",filterArray);
Where key is your searchKeyword, tempArray is your CompleteArray in which data is present.
Use Like this. Please put your data.
Use this
NSPredicate* predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"%# '%#'", #"SELF contains[c] ",searchText]];
To fetch all Cars objects that have a name which is one of the strings in the keys set or array, use
[NSPredicate predicateWithFormat:#"name IN %#", keys]

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.

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 not filtering correctly

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