搜索文件 - Objective-C [英] Searching files - Objective-C
问题描述
我正在尝试搜索文件。这将返回0个结果。有人知道为什么吗?
NSMetadataQuery * q = [[NSMetadataQuery alloc] init];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@kMDItemFSName ==%@,@test123456.png];
[q setPredicate:predicate];
[q startQuery];
while([q isGathering]){
NSLog(@%lu,[q resultCount]);
}
[q stopQuery];
更新
NSMetadataQuery * q = [[NSMetadataQuery alloc] init];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@kMDItemFSName ==%@,@test123456.png];
[q setPredicate:predicate];
[q startQuery];
NSLog(@%lu,[q resultCount]);
[q stopQuery];
感谢
NSPredicate * predicate = [NSPredicate predicateWithFormat:@kMDItemSFName ==%@,@test123456.png];
如果你完全从你的代码中复制,methinks kMDItemSFName
应为 kMDItemFSName
。 ; - )
您使用的是基础工具模板,还是使用运行循环的完整应用程序?
基本上,我相信 MDMetadataQuery
被设计为异步运行,您启动查询,并在查询完成时会通知结果。
对于需要创建
查询并与结果交互
有两个API可用。
Spotlight元数据框架提供
a低级查询API,MDQuery
,
允许应用程序搜索
文件基于元数据值。
MDQuery
完全可配置,
允许您运行同步和
异步查询并提供$ b $
Cocoa框架的
NSMetadataQuery
类为MDQuery
API提供了一个高级的
Objective-C接口。这个类允许你使用
的子集构造查询,NSPredicate
类,并且异步执行
查询 em>。
NSMetadataQuery
支持Cocoa
绑定,允许您显示
结果,而无需编写任何
大量的粘合代码。作为
,NSMetadataQuery
允许
应用程序指定将
分组为多个
子类别的结果。NSMetadataQuery
执行
不支持同步查询和
在收集数据时提供最小更新通知
。 rel =nofollow>技术说明TN2192使用Spotlight查询元数据。I am trying to search for a file. This returns 0 results. Does anyone know why?
NSMetadataQuery *q = [[NSMetadataQuery alloc] init]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"kMDItemFSName == %@", @"test123456.png"]; [q setPredicate:predicate]; [q startQuery]; while ([q isGathering]) { NSLog(@"%lu", [q resultCount]); } [q stopQuery];
update
NSMetadataQuery *q = [[NSMetadataQuery alloc] init]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"kMDItemFSName == %@", @"test123456.png"]; [q setPredicate:predicate]; [q startQuery]; NSLog(@"%lu", [q resultCount]); [q stopQuery];
Thanks
解决方案NSPredicate *predicate = [NSPredicate predicateWithFormat:@"kMDItemSFName == %@", @"test123456.png"];
If you copied that exactly from your code, methinks
kMDItemSFName
should bekMDItemFSName
. ;-)Are you using the foundation tool template, or a full application that will have a run loop?
Basically, I believe
MDMetadataQuery
is designed to be run asynchronously, where you start the query and will be notified of the results when the query has finished.From Introduction to Spotlight Query Programming Guide:
For applications that need to create queries and interact with the results there are two APIs available. The Spotlight metadata framework provides a low-level query API,
MDQuery
, that allows an application to search for files based on metadata values.MDQuery
is completely configurable, allowing you to run synchronous and asynchronous queries and provides fine-grain control of the frequency of results batching.The Cocoa frameworks's
NSMetadataQuery
class provides a high-level Objective-C interface to theMDQuery
API. This class allows you to construct queries using a subset of theNSPredicate
classes, and execute the queries asynchronously.NSMetadataQuery
supports Cocoa bindings, allowing you to display the results without writing any significant amount of glue code. As well,NSMetadataQuery
allows an application to specify the grouping of the results into multiple subcategories.NSMetadataQuery
does not support synchronous queries and provides minimal update notifications as data is collected.See Technical Note TN2192 Querying Metadata With Spotlight.
这篇关于搜索文件 - Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!