1

I would like to figure out the NSPredicate that will search my Core data for words that begins with:

For example: description field in the core data has text like this:

My name is Mike
My name is Moe
My name is Peter
My name is George

If I search 'My name is' I need to get the 3 lines If I search 'My name is M' I need to get the first 2 lines

I tried the code below, but can't get what I need. My guess I need a regular expression, but not sure how to do it.

[NSPredicate predicateWithFormat:@"desc beginswith [cd] %@",word];
nathanchere
  • 8,008
  • 15
  • 65
  • 86
SMA2012
  • 161
  • 1
  • 3
  • 9
  • The predicate is fine. What is the actual problem? – Jorn van Dijk Nov 11 '12 at 22:38
  • well, How can I compare each word at a time as I type it in? If I type in 'My' it needs to find the string with any word begins or matches 'My' then as I type in the second word 'name' it needs to find 'My' and any word starts with 'name' and so forth? – SMA2012 Nov 11 '12 at 22:44
  • Than you should build some methods to give you that result. Maybe you could split the string you type in and use them in the predicate object? I'll update my answer. – yoeriboven Nov 11 '12 at 22:48

1 Answers1

2

I did it this way recently and worked fine. Try it out. I see I don't have [cd] and beginswith but contains. You could give it a try.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@", textField.text];
NSArray *list = [allWords filteredArrayUsingPredicate:predicate];

for(NSString *word in list){
    NSLog(@"%@",word);
}

After reading the comments on the starting post:

First of all, you should split up the words in the string from the textfield:

NSArray *myWords = [textField.text componentsSeparatedByString:@" "];

Then doing the same like you first would:

for(NSString *wordFromTextField in myWords){
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@", wordFromTextField];
    NSArray *list = [allWords filteredArrayUsingPredicate:predicate];

    for(NSString *word in list){
        NSLog(@"%@",word);
    }
}

Instead of NSLogging the words you could add them to an array of course.

yoeriboven
  • 3,541
  • 3
  • 25
  • 40
  • Thanks. I already have it split up. I was doing NSPredicate predicateWithFormat:@"desc beginswith [cd]%@ AND desc beginswith [cd]%@ AND ...",words[0],words[1],words[2]..]. It was only working with first word in array, it stops after hitting space and entering more words. I am not sure how can I accomplish your array loop with one one fetch request? – SMA2012 Nov 11 '12 at 23:09
  • I solved it by playing with your suggestion. I used NSCompoundPredicate. NSMutableArray *subpredicates = [NSMutableArray array]; for(NSString *token in words){ NSPredicate *pred = [NSPredicate predicateWithFormat:@"ColumnName contains[cd] %@",token]; [subpredicates addObject:pred]; } req.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]; I will accept your answer since you led me to the correct solution. – SMA2012 Nov 14 '12 at 03:15