2011-10-23 42 views
0

我想獲取存儲在用戶的iCloud帳戶中的所有txt文件的列表。如果啓用iCloud的檢查後,我試過如下: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) 

我在做什麼錯誤定義謂詞是什麼時候?我已閱讀文檔,但無法弄清楚我出錯的地方。任何幫助將非常感謝!

回答

2

嘗試使用[NSPredicate predicateWithFormat:@"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey]作爲您的謂詞。在這種情況下,SELF不引用文件名。

+0

謝謝!完美工作。我知道這是另外一個問題,但我如何才能獲取文件夾列表?我會與%K ISDIRECTORY一起工作嗎?謝謝。 –