NSPredicate predicateWithFormat for NSMetaDataQuery in iOS

589 views Asked by At

I need to append my custom string into NSPredicate.

So I wrote following codes.

self.query = [[NSMetadataQuery alloc] init];
[self.query setSearchScopes: [NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K like '%@*'",NSMetadataItemFSNameKey,whereAreYou];

NSLog(@"%@",pred.description);

[self.query setPredicate:pred];    

However when I test it , it only return following value.

kMDItemFSName LIKE "%@*"

the placeholder %@ is not append correctly. It only showing %@ sign.

How can I do that?

1

There are 1 answers

0
Martin R On BEST ANSWER

Format arguments inside quotation marks are not expanded by predicateWithFormat, therefore @"%K like '%@*'" does not work.

This should work:

NSString *pattern = [NSString stringWithFormat:@"%@*", whereAreYou];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, pattern];