NSPredicate error with a path that contains square brackets

I need to search for paths that contain square brackets from the main data. But when I try to create a search term like below, xcode fails.

path == /my/local/[path]

I tried to avoid square brackets, from one bracket to 16 brackets, but nothing works.

This path is from another application, so it cannot be changed. what should I do? Thanks for your help in advance!

EDIT:
My code to create is NSPredicate

as follows.
NSPredicate *preTest = [NSPredicate predicateWithFormat:@"localFilePath == /[test]/"];

UPDATE:
I found something strange. NSPredicate *testPre = [NSPredicate predicateWithFormat:@"localFilePath == %@", uploadFile.remotePath];


NSPredicate *what = [NSPredicate predicateWithFormat:@"localFilePath == /"];


Where uploadFile.remotePath is @ "/".
The first one is ok, but the second one fails!

I think this is similar to my problem. Any ideas?
Thanks for allowing your time on my problem! :)

+3


source to share


1 answer


If you need to match [

and ]

, you can use an operator matches

that uses regular expressions . Example:

NSArray *array = @[@"[apple]", @"[boy]", @"[dog]", @"cat"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"self matches %@", @"\\[dog\\]"];
NSLog(@"%@", [array filteredArrayUsingPredicate:pred]);

      


About your update: predicateWithFormat

Doesn't look like it stringWithFormat

, as it does some extra work that helps build the predicate. For example,

[NSPredicate predicateWithFormat@"localFilePath == %@", @"/"]

      



equivalent to

[NSPredicate predicateWithFormat@"localFilePath == \"/\""]

      

You can see the quotes are added automatically. It also predicateWithFormat

accepts some special substitutions like %K

(for dynamic properties).

Apple provides a good tutorial on using predicates that you can test.

0


source







All Articles