searchDisplayController does not display results if the iphone language is non English - iphone

I have a searchDisplayController which works perfectly when searching for English and Arabic words using a method as follows:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSInteger)scope
{
NSString *query = self.searchDisplayController.searchBar.text;
if (query && query.length) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ClientName contains[cd] %#", query];
[searchResultController_.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self searchResultController] performFetch:&error]) {
// Handle error
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
exit(-1); // Fail
}
}
However, this works fine if the iphone language is English, but if I change the iPhone language to Arabic (global settings) and tried to search in Arabic or English words the searchResultsController will not display results, why ?
p.s. when I put a static Arabic word in query like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ClientName contains[cd] %#", #"تجريب"]; the searchDisplayController will display the correct result of the arabic word تجريب
EDIT: I've tried to build the predicate in code like this :
NSExpression *clientNameEx=[NSExpression expressionForKeyPath:#"ClientName"];
NSExpression *aClientEx=[NSExpression expressionForConstantValue:query];
NSPredicate *predicate=[NSComparisonPredicate predicateWithLeftExpression:clientNameEx
rightExpression:aClientEx
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:0];
but to no avail ...

I've made test project with XCode 4, MasterDetail template, CoreData checkbox On and added SearchBarController to the default Master view, added UISearchBarDelegate delegate into the header, and searchBarTextDidEndEditing method to invoke search once done is pressed.
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
[NSFetchedResultsController deleteCacheWithName:nil];
self.fetchedResultsController = nil;
[self.tableView reloadData];
}
Last step was to add into fetchedResultsController:
NSString *query = self.searchDisplayController.searchBar.text;
if (query.length) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"timeStamp contains[cd] %#", query];
NSLog(#"predicate %#", [predicate description]);
[fetchRequest setPredicate:predicate];
}
Switched iPhone Simulator and tested it with iOS5 and Arabic settings. All searches where successful, searching for word "test" and word "تجريب" brings up results.
However, word "تجريب" was copy pasted from your question, as my arabic is little bit rusted :).I've also tried to make record in database with first letter on arabic keyboard and then to search for it and result shows up.

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.

Grouping in search display controller

In my Core Data app, I have an entity Person with names as attribute.
I used following predicate logic to search the names.
It displays names when we search with a word.
well but I want two groups of searched names as
Word containing anywhere in the name
Name starts withe the word.
both groups I want. How?
NSString *searchfilter = [NSString stringWithFormat:#"*%#*",savedSearchTerm_];
NSPredicate *filter = [NSPredicate predicateWithFormat:#"word like[c] %#", searchfilter];
[fetchRequest setPredicate:filter];
Your example should work for #1.
word is anywhere anywhere in the name:
NSString *searchfilter = [NSString stringWithFormat:#"*%#*",savedSearchTerm_];
NSPredicate *filter = [NSPredicate predicateWithFormat:#"word like[c] %#", searchfilter];
[fetchRequest setPredicate:filter];
name starts with the word:
NSString *searchfilter = [NSString stringWithFormat:#"%#*",savedSearchTerm_];
NSPredicate *filter = [NSPredicate predicateWithFormat:#"word like[c] %#", searchfilter];
[fetchRequest setPredicate:filter];
i think you have 2 options here:
1) - (id)initWithFetchRequest:(NSFetchRequest *)fetchRequest managedObjectContext:(NSManagedObjectContext *)context sectionNameKeyPath:(NSString *)sectionNameKeyPath cacheName:(NSString *)name pass sectionNameKeyPath: by what you want to group
2) group after selecting - (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error

CoreData Basics Help

EDIT:
I have altered the NSPredicate as recommended so that my fetch code look like so. Having printed to the UITextView like this, when I press load it spits out the following:
<NSManagedObject: 0x1c7cf0>(entity: DatedText; id: 0x1420c0 <x - coredata://B52D4F88-0210-4AE2-9DA6-C05ED64FE389/DatedText/p12> ; data: <fault>)
So either its not getting any data data because it hasn't been saved/loaded correctly or I am trying to get the loaded result into a UITextView the incorrect way. Any ideas?
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *testEntity = [NSEntityDescription entityForName:#"DatedText" inManagedObjectContext:self.managedObjectContext];
[fetch setEntity:testEntity];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"dateSaved == %#", datePicker.date];
[fetch setPredicate:pred];
NSError *fetchError = nil;
NSArray *fetchedObjs = [self.managedObjectContext executeFetchRequest:fetch error:&fetchError];
if (fetchError != nil) {
NSLog(#" fetchError=%#,details=%#",fetchError,fetchError.userInfo);
return nil;
}
NSString *object = [NSString stringWithFormat:#"%#",[fetchedObjs objectAtIndex:0]];
noteTextView.text = object;
I have been having all sorts of problems working out how to use Core Data, so I have gone back to basics, new window based ipad project using core data.
I have added a view and some code which doesn't work, hehe. Im basically trying to save some text to a date, then when going back to that date, the text which was previously saved will be shown again.
There's a tutorial on iPhone developer site here. And there are several sample codes with Core Data as well. These should get you started.
I checked your project and aside from having to synthesize the CoreData properties, I also just noticed you were trying to assign an NSArray to your fetch predicate, but it actually expects an NSPredicate object. You should use this instead:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"(dateSaved == %#)", datePicker.date];
[fetch setPredicate:pred];
If you want to set more than 1 predicate you should do that on your predicate string i.e.
NSPredicate *pred = [NSPredicate predicateWithFormat:#"(dateSaved == %#) AND (dateSaved <= %#", datePicker.date, [NSDate date]];
Cheers,
Rog
You most likely crashing because your ivar is managedObjectContext_ but you are using self.managedObjectContext. You also need to synthesize the core data ivars even if you provide a custom getter.
You're setting your NSFetchRequest's predicate to an NSArray, not an NSPredicate.
If you had posted the actual crash, it would probably say something like an unknown selector was sent to an instance of NSArray.

Objective-C: NSPredicate LIKE doesn't like spaces. Is there an escape sequence to replace blanks with?

I have a NSPredicate that looks like this:
NSPredicate *likePredicate3= [NSPredicate predicateWithFormat:#"synonyms LIKE[cd] %#",[NSString stringWithFormat:#"%#", searchText]];
I apply it to an NSArray of objects of a class that has the 'synonyms' property.
It works fine when the searchText is a whole word such as "Thanks". However if I try to use strings with space in them such as 'Thank you', it fails and the predicate search does not find the match in the array.
Is there a way to ask NSPredicate to work with words that have a blank space(s) in them?
thanks.
In principle, what you are doing should work. I ran this example:
NSString *search = #"a b";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF LIKE[cd] %#",
[NSString stringWithFormat:#"%#", search]];
NSArray *array = [[NSArray arrayWithObjects:#"a b", #"ä B", #"ccc", nil]
filteredArrayUsingPredicate: predicate];
NSLog(#"result: %#", array);
Output is:
Running…
2010-02-04 20:05:41.770 predicate2[74163:a0f] result: (
"a b",
"\U00e4 b"
)
Maybe your searchString isn't what you think it is...

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.