Why does this predicate returns results:
predicate= [NSPredicate predicateWithFormat:#"itemsString like '*{4160366}*'"];
while this predicate returns empty array
predicate= [NSPredicate predicateWithFormat:#"itemsString like '*{%#}*'",[NSString stringWithString:#"4160366"]];
I's driving me crazy
When building a predicate, predicateWithFormat automatically adds quotes when performing variable substitution. So your (second) predicate ends up looking like this:
itemsString like '*{"4160366"}*'".
Notice the extra set of quotes.
Change it to:
predicate= [NSPredicate predicateWithFormat:#"itemsString like %#",[NSString stringWithFormat:#"*{%#}*", #"4160366"]];
and it should work.
(Note that instead of using stringWithString I just used #"" which does the same thing.)
Related
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"identifer==1_12"];
gives me :
Unable to parse the format string "identifier==1_12"'
I have tried using MATCHES, LIKE, =, with spaces ==, without spaces etc. Somehow I feel the underscore is some kind of a reserved sign.
Any help?
If you want to compare with the string "1_12", you have to enclose it in single quotes:
[NSPredicate predicateWithFormat:#"identifer == '1_12'"]
Alternatively:
[NSPredicate predicateWithFormat:#"identifer == %#", #"1_12"]
In my iPhone app, I'm reading a csv file. The relevant line is this:
NSString *countrycode = [[[NSString alloc] initWithFormat:#"%#", [arr objectAtIndex:2]]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
This returns "CN" (which stands for China).
When I do this:
NSLog(#"Manual: %#, country code: %#",#"CN",countryCode);
I get:
Manual: CN, country code: "CN"
One has quotes and the other does not. I don't know why this is.
The reason this is tripping me up is the following:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"countrycode == %# ", #"CN"];
This works fine, and returns China from Core Data.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"countrycode == %# ", countrycode];
This fails to return anything. I am assuming this is because it has quotes around it, or something, although perhaps I am incorrect.
What am I doing wrong here?
Actually the correct way to format a predicate to exclude quotes is the to use %K versus %#. See Predicate Format String Syntax.
Your countryCode variable must have quotes inside of it when it's read back. The first time you assign the literal #"CN" the quotes are removed as they specify that your variable is an NSString. They aren't really inside of the literal string. If you wanted strings inside of the first CN, you'd need to explicitly specify the quotation marks, e.g. #"""CN"""
However, if you want to get rid of any quotations in the second string, you could always do this to the string prior to putting it into your predicate:
[countryCode stringByReplacingOccurrencesOfString:#"""" withString:#""];
I have an NSArray whose contents are strings with a format similar to:
[A-z]{+}-[0-9]{+}
so basically a bunch of repeating alpha characters, a separator, and then 1 or more digits so
I want to filter by values in the array that match up to the separator but I can't seem to explicitly specify it in my predicator's format:
NSPredicate *aPredicate = [NSPredicate predicateWithFormat:#"self BEGINSWITH %#", aValue];
NSArray *filtered = [entries filteredArrayUsingPredicate:aPredicate];
How do you constrain the filtering for such a case?
You could use the "MATCHES" operator to do a regular expression search, like so:
NSPredicate * p = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", #"[a-z]+-.*"];
NSArray * s = [NSArray arrayWithObject:#"abc-123"];
NSLog(#"%#", [s filteredArrayUsingPredicate:p]);
There is a caveat, though. The regular expression is matched across the entire string. So if you want to find all the elements that begin with 3 letters, your expression can't just be "[a-z]{3}". It has to be "[a-z]{3}.*". The first will fail for anything that's not 3 letters, whereas the second will match anything that's at least 3 letters long.
Took me a while to realize this...
You probably want to use the MATCHES operator that lets you use Regular Expressions.
See Predicate Programming Guide:Regular Expressions
I'm trying to make a predicate that includes special characters
For example:
[[myIngredients filteredSetUsingPredicate:[NSPredicate predicateWithFormat:#"name BEGINSWITH[c] %#", [alphabet objectAtIndex:idx]]];
Here I will get all the ingredient which starts with (let say for idx = 5) 'e'. As I have to do my app in english and french, some ingredients start with special character like 'é' or even 'œ' for 'o'. How can I include these special characters in my predicate?
Best
I think you might be looking for the “diacritic insensitive” flag that NSPredicate supports. It’s just like the “c” flag you’re already using, except you use a “d”. Like so:
… predicateWithFormat:#"name BEGINSWITH[cd] %#", …
Now the string “e” will also match “é”, “ê”, “ë”, and so on.
I have an array of dictionaries. I would like to filter that array by seeing if the #"name" field of each dictionary contains a given string.
The catch is that I would like to make my filtering insensitive to case and diacritics.
If the array contained only strings I could easily use an NSPredicate. However, it doesn't, and I don't see a way that NSPredicate can accomodate this situation.
If I only cared about case-insensitivity, I could loop through all the items and compare the lowercased filter string to the lowercased name. But I don't know of a similar trick for diacritics.
Is there a good solution to this somewhere?
Check the top answer on this question:
Non US characters in section headers for a UITableView
You should be able to use that code to get rid of the diacritics and then do a case insensitive compare or search.
What about something like:
NSArray * array = .....
NSString * searchString = #"foo";
NSArray * filteredArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"name contains[cd] %#", searchString]];