1

我有一个包含文件夹名称的 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"]];

        }
    }

我有这个快速枚举方法,我有数组 SearchData。枚举方法在数组中查找数据,如果未找到该数据,则应将其从数组中删除。SearchData 数组将显示在表格视图中。

我一直在尝试上述方法,但它不起作用。

4

5 回答 5

3
NSString *searchText = @"Posteingang";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FolderName = %@",searchText];
NSMutableArray *predarr = [NSMutableArray arrayWithArray:[yourDATAArray filteredArrayUsingPredicate:predicate]];
if([predarr count]!=0)
{
     NSLog(@"%@",predarr);
}
于 2013-05-02T13:03:40.873 回答
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];

        }

    }    
于 2013-05-03T07:33:29.007 回答
2

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];

    }

 }
于 2013-05-02T13:50:34.987 回答
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];
    }

}

希望它可以帮助你。

于 2013-05-02T13:31:23.090 回答
2
NSString *stringToBeMatched = @"Freigaben";

for (NSDictionary *itemDic in array)
{
     if ([[itemDic objectForKey:@"FolderName"] isEqualToString:stringToBeMatched])
     {
         [dataArray removeObject:itemDic];
     }
}
于 2013-05-02T13:36:38.737 回答