0

我想获取存储在用户 iCloud 帐户中的所有 txt 文件的列表。检查是否启用了 iCloud 后,我尝试了以下操作:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
        _query = query;
        [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]]; 

        NSPredicate *pred = [NSPredicate predicateWithFormat:@"self ENDSWITH '.txt'"];
        [query setPredicate:pred];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
        [query startQuery];

但我得到以下控制台输出和崩溃:

    2011-10-23 21:58:19.587 iCloudText[9922:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSComparisonPredicate with left expression which is not NSKeyPathExpressionType given to NSMetadataQuery (SELF ENDSWITH ".txt")'
*** First throw call stack:
(0x30ef58bf 0x3809a1e5 0x30ef57b9 0x30ef57db 0x37f4e725 0x37f4b0b5 0x29b3 0x2c4b 0x32ec77eb 0x32ec13bd 0x32e8f921 0x32e8f3bf 0x32e8ed2d 0x33a7ae13 0x30ec9553 0x30ec94f5 0x30ec8343 0x30e4b4dd 0x30e4b3a5 0x32ec0457 0x32ebd743 0x2437 0x23dc)
terminate called throwing an exception(gdb) 

定义谓词时我做错了什么?我已经阅读了文档,但无法弄清楚我哪里出错了。任何帮助将不胜感激!

4

1 回答 1

2

尝试使用[NSPredicate predicateWithFormat:@"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey]作为你的谓词。SELF在这种情况下不引用文件名。

于 2011-10-24T00:26:03.293 回答