Question

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?

Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top