我想读到一份由用户或i± 移动文件目录中用户创建的所有名录清单(见Lion中“~/Library/Mobile Documents”下的目录)。 下面是该名录如何看待:
我尝试了以下法规,但问询表一将不包含代表我方的物体(使用国家清单<代码>predicateWithFormat:@%K.pathExtension=“,NSMetadataItemFSNameKey)。 如果我对txt案卷(使用@>%K ENDSWITH .txt”、NSMetadataItemFSNameKey
)提出质询,我将分别收到5个 objects。 因此,查阅txt文档是行之有效的,但不是用于名录。 通过 我注意到, Apple果建议使用NSFileWrapper(碎片包)而不是名录。 i±是否能够处理用户或用户创建的影子?
我的守则是:
-(void)loadDocument {
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
_query = query;
//Search all files in the Documents directories of the application’s iCloud container directories:
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K.pathExtension = ", NSMetadataItemFSNameKey];
[query setPredicate:pred];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
[query startQuery];
}
- (void)queryDidFinishGathering:(NSNotification *)notification {
NSMetadataQuery *query = [notification object];
[query disableUpdates]; // You should invoke this method before iterating over query results that could change due to live updates.
[query stopQuery]; // You would call this function to stop a query that is generating too many results to be useful but still want to access the available results.
[self loadData:query];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
_query = nil; // we re done with it
}
- (void)loadData:(NSMetadataQuery *)query {
NSLog(@"Query count %i", [query resultCount]);
for (int i=0; i < [query resultCount]; i++) {
NSMetadataItem *item = [query resultAtIndex:i];
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
NSLog(@"%i.URL: %@", i, url);
}
}