ユーザーのiCloudフォルダにあるディレクトリのリストを取得しようとしています。特別な種類のファイル(txtファイルなど)を探す方法を考えましたが、正常に機能します。
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 ENDSWITH '.txt'", NSMetadataItemFSNameKey];
[query setPredicate:pred];
[query startQuery];
ただし、現在はディレクトリのみを取得しようとしています。NSPredicateに関するドキュメントを読みましたが、ディレクトリを探す方法がわかりません。NSPredicateはこのために作られていないと思いますか?何かが次のようなディレクトリであるかどうかを確認できます。
BOOL isDir;
BOOL exists = [fm fileExistsAtPath:path isDirectory:&isDir];
if (exists) {
/* file exists */
if (isDir) {
/* file is a directory */
}
}
しかし、これをNSMetadataQueryに適用する方法は、私にはわかりません。私は私が得ることができるどんな助けにも感謝するでしょう。
編集:
述語をに変更しました[NSPredicate predicateWithFormat:@"%K.pathExtension = ''", NSMetadataItemFSNameKey];
次に、クエリがいつ終了するかを次のように判断します。
[[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
}
そして最後に、カウントを行いますが、これにより、クラウドにあるディレクトリの数に関係なく、常に0が得られます(LionおよびMobile Documentsフォルダーを介してこれを確認しました。たとえば、Documents / myTXTなどがあります)。これは非常に奇妙です。テキストファイルを数えると、4つのtxtファイルがあるので4つになります。だから私は私のディレクトリが数えられていないと思います:
- (void)loadData:(NSMetadataQuery *)query {
NSLog(@"Query count %i", [query resultCount]);
...