NSMetaDataQuery,将 setSearchScope 路径设置为递归?

发布于 2024-10-01 10:47:51 字数 487 浏览 1 评论 0原文

在 Objective-C 中,我正在设置一个 NSMetaDataQuery 并设置 setSearchScope: 来搜索特定用户的文档文件夹。搜索有效,但不会在目录树中递归,仅在“文档”文件夹中搜索。
我尝试过通配符但没有乐趣。

这基本上是我尝试过的,除了不在 Documents 目录下搜索之外,它是有效的:

query = [[NSMetadataQuery alloc] init];
[query setSearchScopes:[NSArray arrayWithObjects:@"/Users/username/Documents/",nil]];
[query setPredicate:[NSPredicate predicateWithFormat:@"(kMDItemFSContentChangeDate >= '$time.this_week')"]];
[query startQuery];

In Objective-C, I am setting up an NSMetaDataQuery and setting the setSearchScope: for the query to search a specific users Documents folder. The search works but doesn't recurse down the directory tree, only searching in the Documents folder.
I've tried wildcards but no joy.

Here is basically what I've attempted, and it works except not searching below the Documents directory:

query = [[NSMetadataQuery alloc] init];
[query setSearchScopes:[NSArray arrayWithObjects:@"/Users/username/Documents/",nil]];
[query setPredicate:[NSPredicate predicateWithFormat:@"(kMDItemFSContentChangeDate >= '$time.this_week')"]];
[query startQuery];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林很绿却致人迷途 2024-10-08 10:47:52

我认为您的问题可能是 /Users/username/Documents/ 上的尾部斜杠。当处理 Objective-C 中的路径时,框架从不添加尾部斜杠。去掉有用吗?

I think your problem might be the trailing slash on /Users/username/Documents/. When dealing with paths in Objective-C, the framework never puts on a trailing slash. Does taking it off help?

清秋悲枫 2024-10-08 10:47:51

我认为问题出在您的 NSPredicate 格式字符串上。 (我一直想写下来!)本质上,您的 $time.this_week 在 Spotlight 查询中效果很好,但不能作为 NSPredicate 字符串。您必须实际创建一个 NSDate 对象并将其传递到查询字符串中,

[query setPredicate:[NSPredicate predicateWithFormat:@"(kMDItemFSContentChangeDate <= %@)", [NSDate date]]];

如下所示是供您参考的相关链接:

NSPredicate 和 Spotlight 查询字符串的比较
聚光灯查询表达式语法
谓词格式字符串语法

I think that the problem is with your NSPredicate format string. (I have been meaning to write this up!) Essentially, your $time.this_week would work great in a Spotlight query, but not as a NSPredicate string. You have to actually create an NSDate object and pass it into the query string like

[query setPredicate:[NSPredicate predicateWithFormat:@"(kMDItemFSContentChangeDate <= %@)", [NSDate date]]];

Here are the relevant links for your reference:

Comparison of NSPredicate and Spotlight Query Strings
Spotlight Query Expression Syntax
Predicate Format String Syntax

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文