I have to filter an array of objects by introducing a search keyword and filtering on the run. How am I supposed to do this?
//this is my Article class
@property int intID;
@property (nonatomic, strong) NSString *strName;
@property (nonatomic, strong) NSString *strTitle;
@property (nonatomic, strong) NSString *strDescription;
@property (nonatomic, strong) NSString *lang;
@property (nonatomic, strong) NSString *dateDate;
@property (nonatomic, strong) NSData *dataPicture;
@property (nonatomic, strong) NSString *link;
@property (nonatomic, strong) NSString *gallery;
@property int flag;
@property int subcat;
@property int cat;
@property int idpic;
I have to select article with contains text with introduced string
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"strName MATCHES '.*(%@).*'", searchText];
NSLog(@"%@", [NSString stringWithFormat:@"strName MATCHES '.*%@.*' OR strTitle MATCHES '.*%@.*'", searchText, searchText]);
articleSearchArray=[NSMutableArray array];
articleSearchArray = [NSMutableArray arrayWithArray:[allArticleArr filteredArrayUsingPredicate:predicate]];
for(Article *article in articleSearchArray)
{
NSLog(@"Article id is %i", article.intID);
}
[mytableView reloadData];
}
This one will fetch all the objects in the array that their
strNameproperty containssearchTextstring, using a case-insensitive comparison (this is denoted with the[c]), which I think is what you’re looking for.After that, you just have to set the
NSArraywhich acts as your datasource to the filtered one and do areloadData.