我有一个包含folderName的NSString,我有一个看起来像这样的数组,
DATA (
{
FolderName = Posteingang;
ID = 13000;
},
{
FolderName = Freigaben;
ID = 13001;
},
{
FolderName = "My Drive";
ID = 13002;
},
{
FolderName = gsb;
ID = 13164;
},
{
FolderName = "my folder";
ID = 13183;
}
我想将数组数据与NSString进行比较,以便我可以从字符串中删除不匹配的值。
for (NSString *FileName in ParsedData)
{
NSRange FileNameRange = [FileName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (FileNameRange.location == NSNotFound) {
[SearchData removeObject:[SearchData valueForKey:@"FolderName"]];
}
}
我有这个Fast Enumeration方法,我有Array SearchData。枚举方法查找数组中的数据,如果找不到该数据,则应将其从数组中删除.SearchData数组将显示在tableview中。
我一直在尝试上述方法,但它不起作用。
答案 0 :(得分:3)
NSString *searchText = @"Posteingang";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FolderName = %@",searchText];
NSMutableArray *predarr = [NSMutableArray arrayWithArray:[yourDATAArray filteredArrayUsingPredicate:predicate]];
if([predarr count]!=0)
{
NSLog(@"%@",predarr);
}
答案 1 :(得分:2)
使用此:
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];
}
}
希望它可以帮到你。
答案 2 :(得分:2)
NSString *stringToBeMatched = @"Freigaben";
for (NSDictionary *itemDic in array)
{
if ([[itemDic objectForKey:@"FolderName"] isEqualToString:stringToBeMatched])
{
[dataArray removeObject:itemDic];
}
}
答案 3 :(得分:2)
尝试
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];
编辑:使用快速枚举过滤
for (NSDictionary *dict in [SearchData copy]){
NSString *folderName = dict[@"FolderName"];
if([searchText localizedCaseInsensitiveCompare:folderName]){
[SearchData removeObject:dict];
}
}
答案 4 :(得分:2)
在上述所有答案的一点帮助之后,我自己想出了解决方案
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];
}
}