相关疑难解决方法(0)

使用NSMetadataQuery和NSPredicate返回目录列表

我正在尝试获取用户的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];
Run Code Online (Sandbox Code Playgroud)

但是,我现在只想获取目录.我已经阅读了有关NSPredicate的文档,但我不知道如何寻找目录.我想NSPredicate不是为此而做的?我可以检查是否有像这样的目录:

    BOOL isDir;
BOOL exists = [fm fileExistsAtPath:path isDirectory:&isDir];
if (exists) {
    /* file exists */
    if (isDir) {
        /* file is a directory */
    }
 }
Run Code Online (Sandbox Code Playgroud)

但是如何将它应用于NSMetadataQuery,我不知道.如果能得到任何帮助,我将不胜感激.


编辑:

我将谓词改为 [NSPredicate predicateWithFormat:@"%K.pathExtension = ''", NSMetadataItemFSNameKey];

然后,我确定查询何时完成,如下所示:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nspredicate ios5 icloud

2
推荐指数
1
解决办法
6762
查看次数

标签 统计

icloud ×1

ios5 ×1

iphone ×1

nspredicate ×1

objective-c ×1