Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

predicate for querying a substring of a string from coredata

In my project i have a set of attributes in an entity.One of them is path as string.I want all the records which has my string as a subpath in the path.

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

Mystring : Library/Caches/Packages/1000

I tried using Like and contains as below but failed.

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

Can some one help in writing the predicate to fetch those records which contains mystring.

Really helps me a lot.

Tnx in advance

like image 553
Dinakar Avatar asked Dec 12 '22 21:12

Dinakar


1 Answers

You need to have a predicate like this

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

or this

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

The no results is due to single quotes around your %@. From the documentation (Dynamic Property Names):

string variables are surrounded by quotation marks when they are substituted into a format string using %@

About the predicates I really suggest to use the first, if the sub-path you are looking for it is always in the final part of the original path.

About using predicates, I really suggest to read String Comparisons.

Hope that helps.

like image 161
Lorenzo B Avatar answered Jan 04 '23 23:01

Lorenzo B