NSPredicate to Match the backslash - ios

I want to fetch entity by name matching the string \\xxxx\\yyyy\\zzzz from core data so i have used NSPredicate like NSPredicate *myPredicate = [NSPredicate predicateWithFormat:" name LIKE %#", \\xxxx\\yyyy\\zzzz]; But returns null for some times.I doubt that issue with backslashes so please help me any one.Is any way to fetch this entity from core data?

"LIKE" in a predicate does a simple wildcard matching (with ? and *).
As a consequence, the backslash character has a special meaning and has to be escaped
twice (once for the string literal and once for the "LIKE" operator):
[NSPredicate predicateWithFormat:"name LIKE %#", #"\\\\xxxx\\\\yyyy\\\\zzzz"]
But if you don't need the wildcard matching then use "==" (or "BEGINSWITH", "CONTAINS") instead:
[NSPredicate predicateWithFormat:"name == %#", #"\\xxxx\\yyyy\\zzzz"]

Related

How do I specify an NSPredicate to match strings that don't start with a letter?

This predicate is used to look up CoreData objects based on their title field. I want to fetch any objects that don't start with a letter character (a-z, A-Z).
I have tried this and variations, but I can't get it to work:
[NSPredicate predicateWithFormat:#"%# MATCHES '^[^a-zA-Z]+.*'", #"title"]
Try using %K to use a key path, rather than %# which uses the value:
[NSPredicate predicateWithFormat:#"%K MATCHES '^[^a-zA-Z].*'", #"title"]
(I also dropped the + modifier as that is unnecessary in your pattern.)
HTH

CoreData predicate #"nameOfEntity ==[c] %#"

I got this old project which uses coredata as a persistent store. Problem is it has this kind of predicate for fetching entity:
NSPredicate * predicate = [NSPredicate predicateWithFormat:#"nameOfEntity ==[c] %#", entityName];
I couldn't find what this [c] refers inside this predicate.
Could anyone enlight me into this. I searched everywhere I could.
it means case insensitive. Name, name, NAME will all detected as the same.
String comparisons are by default case and diacritic sensitive. You can modify an operator using the key characters c and d within square braces to specify case and diacritic insensitivity respectively, for example firstName BEGINSWITH[cd] $FIRST_NAME.
source

NSPredicate omits special characters like á â á in filterContentForSearchText method

I am developing an app with an UISearchBar and I need to filter some names with special characters like à, è, ê and so on.. but if I don't type the correct string with the special character it doesn't appear.
Ex: If I am looking for cómodo word I can't find this word if I don't type exactly có
How could I get that word without type that special characters? for example typing just co.
This is my NSPredicate:
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF.word BEGINSWITH[c] %#",
searchText];
Thank you
Ignore diacrtical needs to be specified, "[cd]" instead of "[c]".
See NSHipster: NSPredicate.
NSComparisonPredicate Options:
NSDiacriticInsensitivePredicateOption: A diacritic-insensitive predicate. You represent this option in a predicate format string using a [d] following a string operation (for example, "naïve" like[d] "naive").
Such a common error, it kept me from finding my Metro Station in Paris.

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
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.
Try this:
[NSPredicate predicateWithFormat:#"%K contains %#", #"bookPath", Mystring];

many-to-many string comparison

I am trying to write a predicate similar to
[NSPredicate predicateWithFormat:#"ANY manyNames in %#", allNames];
but instead of 'in' I need 'like'
[NSPredicate predicateWithFormat:#"ANY manyNames like %#", allNames];
This causes an error.
So I want to get results with some objects of manyNames, which are similiar to at least one of allNames.
I solved my problem by joining the strings and making one string of them, then looking for that.

Resources