1
我嘗試在我的Mac上列出我的所有文件,以查看我製作的應用程序。我使用NSMetadataQuery,但它不起作用。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)")
}
}
}
正如你所看到的,我打印metaQuery的結果的數量,並回答了0
我試着改變一些東西像NSMetadataQueryIndexedLocalComputerScope代替NSMetadataQueryUbiquitousDataScope或謂詞的格式,但無論如何它不工作。
任何想法爲什麼?