NSMetaDataQuery,将 setSearchScope 路径设置为递归?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的问题可能是
/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?我认为问题出在您的 NSPredicate 格式字符串上。 (我一直想写下来!)本质上,您的
$time.this_week
在 Spotlight 查询中效果很好,但不能作为 NSPredicate 字符串。您必须实际创建一个 NSDate 对象并将其传递到查询字符串中,如下所示是供您参考的相关链接:
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 likeHere are the relevant links for your reference:
Comparison of NSPredicate and Spotlight Query Strings
Spotlight Query Expression Syntax
Predicate Format String Syntax