n.evermind
n.evermind

Reputation: 12004

iCloud: getting an array of files present in the cloud

I'd like to get a list of all txt files stored in a user's iCloud account. After checking if iCloud is enabled, I tried the following:

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];

But I get the following console output and a crash:

    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) 

What am I doing wrong when defining the predicate? I've read the docs but can't figure out where I went wrong. Any help would be very much appreciated!

Upvotes: 0

Views: 755

Answers (1)

Wilbur Vandrsmith
Wilbur Vandrsmith

Reputation: 5050

Try using [NSPredicate predicateWithFormat:@"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey] as your predicate. SELF doesn't refer to the filename in this case.

Upvotes: 2

Related Questions