Predicate for querying a substring of a string from coredata

In my project, I have a set of attributes in essence. One of them is a path as a string. I need all the records that have my line as a subpath in the path.

Example: Path: / var / mobile / Applications / C4ECC129-36D2-4428-8BB0- 6B1F4C971FC1 / Library / Cache / Packages / 1000

Mystring : Library / Cache / Packages / 1000

I tried using "AND" and contained as below, but could not.

[NSPredicate predicateWithFormat:@"bookPath like[c] '%%%@'",Mystring]; [NSPredicate predicateWithFormat:@"bookPath Contains[cd] '%@'",Mystring]; 

Can someone help writing a predicate to retrieve those records that contain mystring.

It really helps me a lot.

Tnx in advance

+4
source share
2 answers

You should have a predicate like this

 [fecthRequest setPredicate:[NSPredicate predicateWithFormat:@"bookPath endswith[cd] %@", myString]]; 

or

 [fecthRequest setPredicate:[NSPredicate predicateWithFormat:@"bookPath contains[cd] %@", myString]]; 

The results are not associated with single quotes around your %@ . From the documentation ( Dynamic property names ):

string variables are surrounded by quotation marks when they are replaced with a format string using% @

About the predicates that I really suggest using first if the subdirectory you are looking for is always at the end of the source path.

On using predicates, I really recommend reading String Comparison .

Hope this helps.

+12
source

Try the following:

 [NSPredicate predicateWithFormat:@"%K contains %@", @"bookPath", Mystring]; 
0
source

Source: https://habr.com/ru/post/1440778/


All Articles