【问题标题】:Search over all lines in the dictionary using NSPredicate使用 NSPredicate 搜索字典中的所有行
【发布时间】:2013-04-23 06:43:20
【问题描述】:

我的数据结构是:

现在我遍历列表并将“Name”字符串添加到NSMutableArray,然后使用NSPredicate 搜索和过滤数据。

如何搜索整个列表?通过所有数组中的 NameAddress 字符串?

过滤数据:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    searchResults = [sectionsNames filteredArrayUsingPredicate:resultPredicate];
}

加载数据:

#pragma mark -
#pragma mark Load plist file
- (void)loadPList
{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"];
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

        NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
        NSMutableArray *resultArray = [[NSMutableArray alloc] init]; 
        NSMutableArray *resultName = [[NSMutableArray alloc] init];

        sectionKeys = [NSMutableArray new];
        sectionsTitle = [NSMutableArray new];
        sectionsNames = [NSMutableArray new];

        dispatch_async(dispatch_get_main_queue(), ^{


            NSMutableArray *annotations = [[NSMutableArray alloc]init];

            NSMutableArray *annotationsToRemove = [ mapView.annotations mutableCopy ] ;
            [ annotationsToRemove removeObject:mapView.userLocation ] ;
            [ mapView removeAnnotations:annotationsToRemove ] ;

     ann = [dict objectForKey:@"Blue"];
                [resultArray addObject:@"Blue"];
                [resultDic setValue:ann forKey:@"Blue"];
                [sectionKeys addObject:@"Siwa"];


                for(int i = 0; i < [ann count]; i++) {


                    NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];

                    double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
                    double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

                    MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
                    CLLocationCoordinate2D theCoordinate;
                    theCoordinate.latitude = realLatitude;
                    theCoordinate.longitude = realLongitude;

                    myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);
                    myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
                    myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
                    myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];

                    NSString *name = [[ann objectAtIndex:i] objectForKey:@"Name"];
                    [resultName addObject:name];

                    [mapView addAnnotation:myAnnotation];
                    [annotations addObject:myAnnotation];


                }

     self.tableData = resultDic;
            self.sectionsTitle = resultArray;
            self.sectionsNames = resultName;

            [myTable reloadData];


        });



    });


}

【问题讨论】:

    标签: ios search filter nspredicate nsmutabledictionary


    【解决方案1】:

    如果您的字典只包含数组,那么您可以遍历字典中的键并单独过滤每个键(免费编写的代码):

    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"Name CONTAINS[cd] %@ OR Address CONTAINS[cd] %@",
                                    searchText,
                                    searchText];
    
    NSMutableArray *allMatches = [NSMutableArray array];
    
    for (NSString *key in myDictionary) {
        NSArray *array = [myDictionary objectForKey:key];
    
        NSArray *matches = [array filteredArrayUsingPredicate:resultPredicate];
    
        if (matches.count > 0) {
            [allMatches addObjectsFromArray:matches];
        }
    }
    

    这样做,您还可以在需要时访问原始密钥。最后列出所有匹配项(匹配的字典数组)。

    现在,在您的表格视图中,您可以返回行数作为所有匹配项的计数,并从字典中获取 NameAddress 以显示。

    【讨论】:

    • 好的,这行得通...但是我如何在表格中显示过滤后的结果?名称在 cell.textLabel.text 中,地址在 cell.detailTextLabel.text 中?非常感谢!
    • 为什么只搜索最后一个数组?
    • ?代码示例将搜索存储在您的字典中的所有数组。您将不得不解释您遇到的任何问题。
    • 我使用您的代码示例并仅从最后一个数组“蓝色”获取搜索结果 - 查看我的数据结构。不会从“黄色”、“紫色”、“黑色”等中得到结果。
    • NSLog(@"%@", key);我得到了所有数组,但由于某种原因没有从其他数组中得到结果
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多