Why does nspredicate crash? - iphone

I wanted to check if a field contains data or not. Here's my code:
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:
#"( SeriesStudyID == %# )" ,"" ]];
But it crashes. Why?

#iAnand's answer is wrong. Using %# in a predicate format string is perfectly acceptable.
The problem is that %# means to substitute in an object, but you're substituting in "", which is not an object, but a char*. Thus, you need to simply add an # sign in front of the double quotes to turn it from a char* into an NSString.

Related

NSPredicate with Underscore Crash

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"]

NSPredicate, with quotes / without quotes

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:#""];

NSPredicates beginsWith

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

Regular expression for numbers

I need a regular expression to detect at least one number in a string. Other characters can be anything. Please help me to implement this in objective C.
Regards,
Dilshan
\d+
Match one or more digit.
This is a very similar question to:
Regular Expressions in Objective-C and Core Data
Check ICU Regex Documentation for figuring out your regular expression needs
To match a digit anywhere in string use .*\\d.*. To implement in objective-c use NSPredicate try something like this:
NSString *matchphrase = #".*\\d.*";
BOOL match = NO;
NSString *item = #"string with d1g1it";
NSPredicate *matchPred = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", matchphrase];
match = [matchPred evaluateWithObject:item];
More here
Edited according Dislhan comment.

NSPredicate of special characters - iPhone

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.