1
votes

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
what type of whereAreYou? - Jake Lin
yes that is String and i am sure it has value. - Fire Fist
I suggest you try NSString stringWithFormat first. And also look at developer.apple.com/library/mac/documentation/cocoa/conceptual/… - Jake Lin
No bro . i have already tried like you said. - Fire Fist

1 Answers

4
votes

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