gpt4 book ai didi

objective-c - 搜索文件 - Objective-C

转载 作者:行者123 更新时间:2023-12-03 16:59:25 26 4
gpt4 key购买 nike

我正在尝试搜索文件。这将返回 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"];  

如果您完全从代码中复制了该内容,我认为 kMDItemSFName 应该是 kMDItemFSName。 ;-)

您使用的是基础工具模板,还是具有运行循环的完整应用程序?

基本上,我相信 MDMetadataQuery 被设计为异步运行,您可以在其中启动查询,并在查询完成时收到结果通知。

来自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 the MDQuery API. This class allows you to construct queries using a subset of the NSPredicate 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.

参见 Technical Note TN2192 Querying Metadata With Spotlight

关于objective-c - 搜索文件 - Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6398546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com