1

I want to search my eventList Array based on multiple parameters like title,enddate etc... I am using follwing code when i use only one parameter titile like[cd] %@ it works perfectly

        NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimmed = [searchBar.text stringByTrimmingCharactersInSet:whitespace];
        if([trimmed isEqualToString:@""]==NO)
        {
            //        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title like[cd] %@",[NSString stringWithFormat:@"*%@*", searchBar.text]];
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title like[cd] %@ ||  enddate like[cd] %@  ",[NSString stringWithFormat:@"*%@*", searchBar.text],[NSString stringWithFormat:@"*%@*", searchBar.text]];

            NSLog(@"Main Array:%@",arrMain);
            eventsList =(NSMutableArray*) [arrMain filteredArrayUsingPredicate:predicate];
            [eventsList retain];
            NSLog(@"Search Result:%@",eventsList);
            [eventTable reloadData];
        }

i have tried with
NSPredicate predicate = [NSPredicate predicateWithFormat:@"(title like[cd] %@) OR (enddate like[cd] %@) ",[NSString stringWithFormat:@"%@", searchBar.text],[NSString stringWithFormat:@"%@*", searchBar.text]];

and it works also but not exact as i am searching for specific date it will not show but if i write 2 or p or a it works for some result only...so i can say its not perfect and my enddate is string with format 2012-09-04 18:28:02 can any one help me with this....

Anand
  • 1,129
  • 7
  • 29
  • i have tried with NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(title like[cd] %@) OR (enddate like[cd] %@) ",[NSString stringWithFormat:@"*%@*", searchBar.text],[NSString stringWithFormat:@"*%@*", searchBar.text]]; – Anand Sep 04 '12 at 17:02

2 Answers2

3

Assemble an appropriate collection of individual predicates into NSCompoundPredicate

as in

NSMutableArray *parr = [NSMutableArray array];
if([brand_one length]) { 
    [parr addObject:[NSPredicate predicateWithFormat:@"(title like[cd] %@) OR (enddate like[cd] %@) ",[NSString stringWithFormat:@"*%@*", searchBar.text],[NSString stringWithFormat:@"*%@*", searchBar.text]]];
}
if([brand_two length]) { 
     // etc 
}

NSCompoundPredicate *cpred = [NSCompoundPredicate andPredicateWithSubpredicates:parr];

You can stack up your predicates with orPredicateWithSubpredicates: and andPredicateWithSubpredicates: and NSCompoundPredicate being a NSPredicate itself can be compounded.

See http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSCompoundPredicate_Class/Reference/Reference.html

prashant
  • 1,920
  • 14
  • 26
0

Done with

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(title like[cd] %@) OR (enddate like[cd] %@) ",[NSString stringWithFormat:@"*%@*", searchBar.text],[NSString stringWithFormat:@"*%@*", searchBar.text]];

i have checked my database it contains date in 2012-09-04 18:28:02 format and i was displaying in some other format.

Anand
  • 1,129
  • 7
  • 29