NSPredicate Filtering with special characters - iphone

I am using the code below to filter the array using the predicate.
self.aPredicateString = [self.aPredicateString stringByAppendingString:[NSString stringWithFormat:#"%# contains[cd] '%#'", self.selectedSearchParameter, self.searchString]];
NSLog(#"Predicate string %#",self.aPredicateString);
self.aPredicate = [NSPredicate predicateWithFormat:self.aPredicateString];
If user enters either \ or ' the code crashes with error.
I do not understand what the mistake is? Either special characters are not allowed in NSPredicate or should I format the predicate in different way.

I wonder, why you use all this self.aPredicateString stringByAppendingString, but I think, your predicate should look like this:
self.aPredicate = [NSPredicate predicateWithFormat:#"%# contains[cd] %#", self.selectedSearchParameter, self.searchString]];
with no '' around the %#

Related

Multiple string variables in a function ios

I'm really new to ios and i'm trying to write a function that recieves 4 string,
Heres what I try:
this is in my h file:
-(BOOL)validate:(NSString*)fullname:(NSString*)email: NSString username:(NSString*)password
this is in my m file:
-(BOOL)validate:(NSString*)fullname:(NSString*)email: NSString username:(NSString*)password
{
NSString *emailRegex = #"[A-Z0-9A-z._%+- ]+#[A-Za-z0-0.-]+\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#",emailRegex];
return YES;
}
the errors is get are that iam using an undeclared identifier
on the emailRegex
and on the emaitTest
Basicly all i'm trying to do is declare a method that recieves 4 strings:
fullname
email
username
password
and implement it.
any help would be good.
good day.
It should be as :
-(BOOL)validateFullName:(NSString*)fullName email:(NSString*)email userName:(NSString*)username withPassword:(NSString*)password;
For Better readability :
-(BOOL)validateFullName:(NSString*)fullName
email:(NSString*)email
userName:(NSString*)username
password:(NSString*)password;
*Edit: Naming convention was not best, so renamed.

NSPredicate: how to do NOT EndsWith?

I have a list of images in the app bundle and need to display appropriate ones in app, the image has the format:
###_photo_#.jpg
###: toy id, from 1000 to 2000
#: photo id, from 1
so for example
1000_photo_1.jpg
1000_photo_2.jpg
I used to get the list of files in the bundle and use a predicate to filter out other files:
#"self ENDSWITH '.jpg' AND self BEGINSWITH '%d_photo_'", toyId
but now there are retina images which end with #2x.jpg, so this method need to be fixed, I am thinking about adding:
NOT ENDSWITH '#2x.jpg'
but is this correct? should I say:
NOT (ENDSWITH '#2x.jpg')
or:
(NOT ENDSWITH '#2x.jpg')
instead?
You can use a predicate string like this:
#"(self ENDSWITH '.jpg') AND NOT (self ENDSWITH '#2x.jpg') AND (self BEGINSWITH '%d_photo_')"
You can encapsulate a predicate in another predicate:
NSPredicate *positivePredicate = [NSPredicate ...];
NSPredicate *negativePredicate = [NSCompoundPredicate notPredicateWithSubpredicate: positivePredicate];
This allows you to preserve the existing legible format string. Note that with NSCompoundPredicate, you can also build AND and OR predicates. From these three (AND, OR, NOT), you can even derive things like XNOR and NAND predicates (though how to do so is an exercise left to the reader...)
I think a better option in iOS 4.0+ is to use NSPredicate predicateWithBlock: to define your conditions. That way you can use standard NSString functions like hasSuffix: to check for your endsWith negative case.
Checkout a good tutorial here: http://www.wannabegeek.com/?p=149
Here's a basic way you could use it.
NSInteger toyId = 10;
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject hasSuffix:#".jpg"] &&
![evaluatedObject hasSuffix:#"#2x.jpg"] &&
[evaluatedObject hasPrefix:[NSString stringWithFormat:#"%#_photo_", [NSNumber numberWithInt:toyId]]];
}];
You can then grab your array of files
[arrayOfFiles filterArrayUsingPredicate:predicate];
Try this:
NOT something ENDSWITH '#2x.jpg'

Regular expression to allow only string and number

I'm making the pattern to check the user can only enter number and string. Please anyone can help me or give some reference. Following is my code. Thnks in advance.
NSString *regEx =
#"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
#"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
#"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-z0-9](?:[a-"
#"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
#"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
#"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
#"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSString *strRoadworthyGet=#"vinod123";
NSPredicate *matchPattern =[NSPredicate predicateWithFormat:#"SELF MATCHES %#",regEx];
BOOL resultMatch =[matchPattern evaluateWithObject:strRoadworthyGet];
if(!resultMatch)
NSLog(#"invalid pattern");
You can use NSCharacterSet's alphanumeric method to return check for Letters, Marks and Numbers.
If you want to go for RegularExpression, you can use
NSString *regEx = #"^[a-zA-Z0-9]*$";
Defining legal input characters

Validate URL on iPhone

I was trying to validate URL links using regex, but not all the links are being completely identified. Can you please help me out?
I want the links to follow the pattern:
http://www.abcdef.org/xyz/content.aspx?menu id=190&id=3214
Assuming you are looking for a regular expression to match urls with a specific pattern:
You can use something like this to match http://www.abcdef.org/xyz/content.aspx?menu id=190&id=3214:
http://.*?/[a-zA-z]+/content.aspx\?menu id=\d+?&id=\d+
Regex for url with port in Objective c
It works well if don't have port number.
-(BOOL) validateUrl: (NSString *) candidate {
NSString *urlRegEx = #"^(http|https|ftp)\://(([a-zA-Z0-9-.]+\.[a-zA-Z]{2,3})|([0-2]*\d*\d\.[0-2]*\d*\d\.[0-2]*\d*\d\.[0-2]*\d*\d))(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9-._\?\,\'/\+&%\$#\=~])*[^.\,)(\s]$";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}

iPhone Objective-C Form Validation

I am an old developer but new to Objective-C and iPhone development. I am looking for the best way to do form validation for values entered. I have been googling for a while and can't find any good code, but might be using the wrong key worrds, etc... I am looking for things like catching for empty string, numeric validation, date validation, etc... where it catches this either while the user is entering data (input mask type) or on the lost focus of the control.
If you can point me to some good resources that would be great!
Thanks!
Simon.
I don't know particular resources but you could begin usin NSPredicate to validate fields with regular expressions. For example, for mail validation
BOOL result;
NSString* emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate* emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
result = [emailTest evaluateWithObject:anEmailAddress];
The emailRegex suggested by Espuz has a slight problem. The range operator "-" within the [] brackets should be escaped otherwise emails having "-" character are returned as invalid.
I am using the following regex string for pattern matching that allows "_", "-" and "." characters in the address:
NSString *emailRegex = #"[a-zA-Z0-9.\\-_]{2,32}#[a-zA-Z0-9.\\-_]{2,32}\.[A-Za-z]{2,4}";
NSPredicate *regExPredicate =
[NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
BOOL validEmail = [regExPredicate evaluateWithObject:txtField.text];