gpt4 book ai didi

swift - NSMetadataQuery 不返回任何数据

转载 作者:行者123 更新时间:2023-11-28 06:37:49 25 4
gpt4 key购买 nike

我尝试在我的 Mac 上列出我制作的应用程序的所有文件。我使用 NSMetadataQuery 但它不起作用。

代码如下:

import Cocoa

class ViewController: NSViewController
{

let metadataQuery = NSMetadataQuery()

@IBOutlet weak var searchTextField: NSTextField!

@IBOutlet weak var labelML: NSTextField!

@IBAction func searchClick(sender: AnyObject)
{
labelML.stringValue = "Hello \(searchTextField.stringValue)!"
startQuery()
handleMetadataQueryFinished(metadataQuery)
}

override func viewDidLoad()
{
super.viewDidLoad()

// Do any additional setup after loading the view.
}


func startQuery()
{
print("Starting the query now...")

metadataQuery.searchScopes = [NSMetadataQueryUbiquitousDataScope]
let predicate = NSPredicate(format: "%K ==[cd] '*'", NSMetadataItemFSNameKey)

metadataQuery.predicate = predicate
if metadataQuery.startQuery(){
print("Successfully started the query.")
} else {
print("Failed to start the query.")
}

}

func handleMetadataQueryFinished(sender: NSMetadataQuery)
{

print("Search finished");
metadataQuery.disableUpdates()
metadataQuery.stopQuery()
print("Number of results \(metadataQuery.resultCount)")

for item in metadataQuery.results as! [NSMetadataItem]
{

let itemName = item.valueForAttribute(NSMetadataItemFSNameKey)
as! String

let itemUrl = item.valueForAttribute(NSMetadataItemURLKey)
as! NSURL

let itemSize = item.valueForAttribute(NSMetadataItemFSSizeKey)
as! Int
print("Item name = \(itemName)")
print("Item url = \(itemUrl)")
print("Item size = \(itemSize)")

}

}

}

如您所见,我打印了元查询的结果数,结果为 0。

我尝试更改一些东西,例如 NSMetadataQueryIndexedLocalComputerScope 而不是 NSMetadataQueryUbiquitousDataScope 或谓词的格式,但无论哪种方式都不起作用。

知道为什么吗?

最佳答案

你应该为 NSMetadataQueryDidFinishGatheringNotification 注册一个观察者并等待它被调用。搜索需要一点时间。开始查询是否返回 true

这是我的代码中的一些 Objective-C 风格示例:

#import "CloudUtils.h"

@interface CloudUtils ()
@property(nonatomic, strong) NSMetadataQuery *query;
@end

@implementation CloudUtils

static CloudUtils *singleton;

+ (CloudUtils *) sharedInstance
{
if (singleton == nil) {
singleton = [[CloudUtils alloc] init];
}
return singleton;
}

+ (void) updateCloudDrive
{
NSLog(@"in updateCloudDrive");

CloudUtils *utils = [CloudUtils sharedInstance];

// Wichtig: Das Query muss STRONG gebunden sein... sonst ist das zu früh wieder weg!
utils.query = [[NSMetadataQuery alloc] init];
utils.query.searchScopes = [NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope, NSMetadataQueryUbiquitousDataScope,nil];
utils.query.predicate = [NSPredicate predicateWithFormat:@"%K like[cd] %@", NSMetadataItemFSNameKey, @"*"];

[[NSNotificationCenter defaultCenter] addObserver:utils
selector:@selector(queryDidFinishGathering:)
name:NSMetadataQueryDidFinishGatheringNotification
object:utils.query];

[[NSNotificationCenter defaultCenter] addObserver:utils
selector:@selector(queryDidUpdate:)
name:NSMetadataQueryDidUpdateNotification
object:utils.query];

dispatch_async(dispatch_get_main_queue(), ^{
// Das scheitert, falls schon ein solches Query läuft... was aber nicht schlimm ist.
[utils.query startQuery];
});
}

// Diese Methode kommt ins Spiel, wenn es zu viele Ergebnisse auf einmal sind...
// Dann werden einige davon schon gemeldet, bevor das Query ganz fertig ist...
- (void) queryDidUpdate: (NSNotification *) notification
{
NSLog(@"in queryDidUpdate:");

NSMetadataQuery *query = [notification object];
[query disableUpdates];
NSError *error = nil;
for (NSMetadataItem *item in [query results]) {
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
NSLog(@"starting download of %@", url);
[[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:url error:&error];
}
[query enableUpdates];
}

- (void) queryDidFinishGathering: (NSNotification *) notification
{
NSLog(@"in queryDidFinishGathering:");

NSMetadataQuery *query = [notification object];
[query disableUpdates];
[query stopQuery];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidUpdateNotification object:query];

NSError *error = nil;
for (NSMetadataItem *item in [query results]) {
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
NSLog(@"starting download of %@", url);
[[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:url error:&error];
}
}

@end

关于swift - NSMetadataQuery 不返回任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38698067/

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