Reputation: 178
I have an NSString which contains the folderName and i have an array which looks like this,
DATA (
{
FolderName = Posteingang;
ID = 13000;
},
{
FolderName = Freigaben;
ID = 13001;
},
{
FolderName = "My Drive";
ID = 13002;
},
{
FolderName = gsb;
ID = 13164;
},
{
FolderName = "my folder";
ID = 13183;
}
I would like to compare the array data with the NSString so i can remove the values from the string that do not match.
for (NSString *FileName in ParsedData)
{
NSRange FileNameRange = [FileName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (FileNameRange.location == NSNotFound) {
[SearchData removeObject:[SearchData valueForKey:@"FolderName"]];
}
}
I have this Fast Enumeration method and i have the Array SearchData. The enumeration method looks for the data in the array and if that data is not found then it should remove it from the array.The SearchData array is to be displayed in a tableview.
I have been trying the above method but it doesn't work.
Upvotes: 1
Views: 544
Reputation: 10201
Try
NSArray *folders = @[@{@"FolderName":@"Posteingang",@"ID":@13000},
@{@"FolderName":@"Freigaben",@"ID":@13001},
@{@"FolderName":@"My Drive",@"ID":@13002},
@{@"FolderName":@"gsb",@"ID":@13164},
@{@"FolderName":@"my folder",@"ID":@13183}];
NSString *searchText = @"gsB";
//Choose a predicate that you want
//Case insensitive and diacritic search
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FolderName LIKE [cd] %@",searchText];
//Case sensitive search
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FolderName = %@",searchText];
//Any Matches
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FolderName CONTAINS [cd] %@",searchText];
NSArray *filteredArray = [folders filteredArrayUsingPredicate:predicate];
EDIT : Filtering using fast enumeration
for (NSDictionary *dict in [SearchData copy]){
NSString *folderName = dict[@"FolderName"];
if([searchText localizedCaseInsensitiveCompare:folderName]){
[SearchData removeObject:dict];
}
}
Upvotes: 2
Reputation: 178
After a little help from all the answers above i figured out the solution on my own
for (NSDictionary *dict in [SearchData copy]){
NSString *folderName = [dict objectForKey:@"FolderName"];
NSRange range= [folderName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(range.location == NSNotFound){
[SearchData removeObject:dict];
}
}
Upvotes: 2
Reputation: 2145
NSString *stringToBeMatched = @"Freigaben";
for (NSDictionary *itemDic in array)
{
if ([[itemDic objectForKey:@"FolderName"] isEqualToString:stringToBeMatched])
{
[dataArray removeObject:itemDic];
}
}
Upvotes: 2
Reputation: 9913
Use this :
NSString *matchString = @"Posteingang";
for (int i = 0; i > [dataArray count]; i++) // Here dataArray is your array
{
NSMutableDictionary *allDict = [dataArray objectAtIndex:i];
if ([[allDict objectForKey:@"FolderName"] isEqualToString:matchString]) {
[dataArray removeObjectAtIndex:i];
}
}
hope it helps you.
Upvotes: 2
Reputation: 4750
NSString *searchText = @"Posteingang";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FolderName = %@",searchText];
NSMutableArray *predarr = [NSMutableArray arrayWithArray:[yourDATAArray filteredArrayUsingPredicate:predicate]];
if([predarr count]!=0)
{
NSLog(@"%@",predarr);
}
Upvotes: 3