How do I Add a searchbar with NSPredicate - iphone

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.

Related

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

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

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

Use NSPredicate to filter by object attribute

I have a mutable array of custom objects. I want to filter that array by attribute of the object, for example myObject.attributeOne.
How can I create the NSPredicate to use with
[myArrayOfObjects filterUsingPredicate:<the_predicate>]
Use it in this way:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"FriendStatus == 1"];
NSMutableArray *filtered = [MessageArray filteredArrayUsingPredicate:predicate];

Querying Core Data with Predicates - iPhone

So I'm trying to fetch objects from core data. I have list of say 80 objects, and I want to be able to search through them using a UISearchBar. They are displayed in a table.
Using the apple documentation on predicates, I've put the following code in one
of the UISearchBar delegate methods.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (self.searchBar.text !=nil)
{
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"name LIKE %#", self.searchBar.text];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
else
{
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"All"];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort(); // Fail
}
[self.tableView reloadData];
[searchBar resignFirstResponder];
[_shadeView setAlpha:0.0f];
}
If I type in the search field an exact match to the name property of one of those objects, the search works, and it repopulates the table with a single cell with the name of the object. If I don't search the name exact, I end up with no results.
Any Thoughts?
It seems as though iPhone doesn't like the LIKE operator. I replaced it with 'contains[cd]' and it works the way I want it to.
use contains[cd] instead of like, and change:
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"All"];
to:
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"1=1"];
Did you try it using MATCH and regular expressions? Just curious to see if LIKE is something that should be avoided on the iPhone or not...
In typical Core Data application one should delete NSF fetchedResultsController:
[NSFetchedResultsController deleteCacheWithName: [self.fetchedResultsController cacheName]];
Or else you'll get an exception (ideally) or you'll have strange behavior.