1

I have an array of objects. Object body is a sentence, and i want to filter my UITableView by the word of this sentence.

For example, I have to objects:

Id: 1; Body: "Hello my dear friend";

Id: 2; Body: "Frodo, give me the ring!";

And if i begin to enter "Fr" UISearchControler must return both of objects, because both contains the words begins with "Fr" ("frodo, friend"). Then if I enter "Fro" it will return second object.

Now I'm splitting the messages into array of words, and filterring them with

@"self.body contains[c]%@", searchString 

predicate.

How can i combine contains and beginwith predicates?

Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
Arthur
  • 1,740
  • 3
  • 16
  • 36
  • http://stackoverflow.com/questions/2753956/how-do-i-check-if-a-string-contains-another-string-in-objective-c May be this can be helpful , You might want to use `NSRange` for this purpose. – Bhumit Mehta Oct 13 '14 at 07:17
  • This post helps to know - does the string contains? But how to filter my arrayOfObject to filterredArrray? I think I heve to use predicates, but I don't know how to configure it for my purpose ) – Arthur Oct 13 '14 at 07:21
  • Yes With `NSRange` way you will have to manually do a loop . – Bhumit Mehta Oct 13 '14 at 07:45

2 Answers2

1

you can write this code in SearchBar Delegate method

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText   
{ 
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains%@",searchTxt.uppercaseString];
   NSArray* FilteredArr = [objTableData filteredArrayUsingPredicate:predicate];

}

also take care about cases(lowercase and uppercase )

karthikeyan
  • 3,821
  • 3
  • 22
  • 45
Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
0

Code example below:

 NSArray* words =  [body componentsSeparatedByString:@" "];

 NSArray* seacrhedWords = [words filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self BEGINSWITH[c] %@", seacrhText]];
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
Avi
  • 11
  • 3