2016-11-30 2 views
0

Die NSMetadataQuery Klasse scheint zu sein, wie Finder/Spotlight über ihre Metadaten nach Dateien sucht.Wie filtert man ein NSArray von Datei-URLs durch Spotlight File Metadatenattribute/NSMetadataQuery?

NSMetadataQuery class provided by the Foundation framework. Queries can be run in two modes: asynchronous, and asynchronous with live updates. The first simply performs the search on the files that exist at the time of the initial search. The latter continues to search. updating the data as the files that fulfill or no longer fulfill the search parameters update.

https://developer.apple.com/library/content/documentation/Carbon/Conceptual/SpotlightQuery/Concepts/Introduction.html#//apple_ref/doc/uid/TP40001843-BBCFBCAG

Allerdings scheint es orientiert sich an ein Verzeichnis (searchScopes) Bereitstellen und dann Ergebnisse asynchron zurückkehrt, die in diesen Verzeichnissen (NSMetadataQueryDidFinishGathering) gefunden wurden.

Ich habe bereits eine NSArray mit Datei-URLs. Ich möchte einen Filter/Suche dieser NSURLs mit der gleichen Metadaten und Abfragesyntax wie eine Spotlight Search erstellen. Aber ich werde eine Liste von Dateien zum schnellen Filer bereitstellen, anstatt ein Verzeichnis bereitzustellen und asynchrone Ergebnisse zu erhalten.

// Something like this... 
let imageFileTypePredicate = NSPredicate(fromMetadataQueryString: "(kMDItemGroupId = 13)") 
let imageURLs = allURLs.filter{ imageFileTypePredicate.evaluate(with:$0) }; 

jedoch, dass eine Standard-NSPredicate Suche verwendet, anstatt eine Filter Datei-Metadaten und wirft den Fehler:

this class is not key value coding-compliant for the key _kMDItemGroupId.

Die Spotlight-Metadaten-Attribut Ich habe Interesse an Filterung von hier aufgeführt sind:
https://developer.apple.com/library/content/documentation/CoreServices/Reference/MetadataAttributesRef/Reference/CommonAttrs.html#//apple_ref/doc/uid/TP40001694-SW1

Wie kann ein Array von Datei-URLs durch Spotlight-Metadaten gefiltert werden?

Antwort

0

Erstellen Sie ein MDItem für jede URL, um die Spotlight-Attribute der Datei abzurufen.

MDItem is a CF-compliant object that represents a file and the metadata associated with the file.

https://developer.apple.com/reference/coreservices/1658213-mditem

MDItemRef item = MDItemCreateWithURL(kCFAllocatorDefault, url); 
CFArrayRef attributes = MDItemCopyAttributeNames(item); 
NSDictionary *attributeValues = CFBridgingRelease(MDItemCopyAttributes(item, attributes)); 
Verwandte Themen