objective-c - 通过使用 NSMetadataQuery 构造 NSPredicate 来查找具有聚光灯的不可见文件夹

标签 objective-c cocoa predicate spotlight

我正在构建一个 NSmetaDataQuery 来查找不可见文件夹(例如“.myInvisibleFolder”)。

不幸的是,聚光灯似乎无法定位以“.”开头的文件夹,即使特别包含在谓词中也是如此。

什么有效,什么无效

搜索任何不可见的文件名都有效。

搜索内容有效(kMDItemTextContent)。

没有以“.”开头的文件曾经被发现过。始终返回 0 个结果。

作为测试,在 Finder 中搜索不可见内容是有效的。

我做错了什么?还有其他方法可以找到不可见的文件夹吗?

代码:

- (void)searchForMyInvisableFolders{
    self.query = [[[NSMetadataQuery alloc] init] autorelease];

    // To watch results send by the query, add an observer to the NSNotificationCenter
    NSNotificationCenter *nf = [NSNotificationCenter defaultCenter];
    [nf addObserver:self selector:@selector(queryNote:) name:nil object:self.query];

    // Sort results by file name
    [self.query setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:(id)kMDItemFSName ascending:YES] autorelease]]];

    [self.query setDelegate:self];

    //Create a predicate to search for file name
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@" (kMDItemFSName == '.myInvisibleFolder')"];

    //Create a predicate to search for invisible files
    NSPredicate* invisablePredicate = [NSPredicate predicateWithFormat:@"kMDItemFSInvisible == YES"];

    //Compound predicate
    NSPredicate* compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate, invisiblePredicate, nil]]; 

    // Set it to the query.
    [self.query setPredicate:compoundPredicate];           

    // Start it.
    [self.query startQuery]; 

}   

最佳答案

如果我将第一个谓词更改为:

,您的代码非常适合我
[NSPredicate predicateWithFormat:@" (kMDItemFSName == '.DS_Store')"];

您的隐形文件夹是否真的名为“.myInvisableFolder”(请注意您的无形文件夹的拼写错误)?

关于objective-c - 通过使用 NSMetadataQuery 构造 NSPredicate 来查找具有聚光灯的不可见文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1441466/

相关文章:

ios - 以编程方式锁定设备屏幕

objective-c - 找不到托管对象模型?

cocoa - NSTextView 中的硬包装

java - Java 8 中的谓词和函数接口(interface)有什么区别?

objective-c - iOS 8 快照尚未呈现的 View 会导致空快照

ios - Cocos2d-iOS 中的设备方向

ios - 我们如何使用 OpenCV 检测人脸

objective-c - NSAlert - 你可以有一个自定义图标吗?

java - 通过两个参数进行流过滤

iOS核心数据: Fetch child Magical Record data