Horatiu Paraschiv
Horatiu Paraschiv

Reputation: 1780

How to create a NSPredicate which filters all characters in given range including digits/numbers?

I want to implement full text search and what I do is this:

NSString* start = @"a";
NSString* stop = [start stringByAppendingString:@"zzz"];
NSArray* range = [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:start],[NSExpression expressionForConstantValue:stop],nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY keyword.word BETWEEN %@",range];

Start is variable and changes depending what I type (i.e. if I type 'cool' it means start= @"cool" and stop will be @"coolzzz").

Now this works great until my input is a digit or number and the NSPredicate doesn't return any results even though there are.

For example in my data I have entries of shape entry_name 1, entry_name 2. If I input entry_name I get all results but as soon as I input entry_name 1 I get no results.

After testing I've seen that if I just enter 1 I get no results so the NSPredicate doesn't work.

How can I write an NSPredicate which filters all the characters including digits?

Upvotes: 0

Views: 472

Answers (2)

Elise van Looij
Elise van Looij

Reputation: 4232

You don't need to create a range for the BETWEEN operator, a simple array is enough:

NSString* start = @"a";
NSString* stop = [start stringByAppendingString:@"zzz"];
NSPredicate *betweenPredicate = [NSPredicate predicateWithFormat: @"attributeName BETWEEN %@",
                                     [NSArray arrayWithObjects:start, stop, nil]];

See also Predicate Programming Guide

Upvotes: 1

K. Jaiswal
K. Jaiswal

Reputation: 208

    NSString *searchText=@"string u want to search";

NSArray *range = [NSArray arrayWithObjects:@"entry_name 1",@"entry_name 2",@"entry_name 3",@"entry_name 4",@"entry_name5",nil];
NSMutableArray *match = [[NSMutableArray alloc] init];
for (int i=0; i<[range count];i++)
{
    NSString *sTemp = [range objectAtIndex:i] ;
    NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

    if (titleResultsRange.length > 0)
        [match addObject:sTemp];
}
 NSLog(@"match= %@",match);

 use above code for searching.

Upvotes: 0

Related Questions