Reputation: 1076
This is what I got
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object <UITextField: 0x7165730; frame = (121 208; 179 30); text = '[email protected]'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7166ca0>; layer = <CALayer: 0x715ed50>>.'
*** First throw call stack:
(0x1c94012 0x10d1e7e 0xbfbb5e 0xb38bae 0xb383dc 0xb37e9a 0x3c04 0x29a9 0x10e5705 0x1c920 0x1c8b8 0xdd671 0xddbcf 0xdcd38 0x4c33f 0x4c552 0x2a3aa 0x1bcf8 0x1befdf9 0x1befad0 0x1c09bf5 0x1c09962 0x1c3abb6 0x1c39f44 0x1c39e1b 0x1bee7e3 0x1bee668 0x1965c 0x1f95 0x1e95)
libc++abi.dylib: terminate called throwing an exception
Sorry if too big, but I don't understand that error, I disabled from .xib Autolayout because that made my App crash in old devices, and my app never crashed in that TextField, is the textfield for emails.
Thanks in advance.
EDIT:
My Code:
NSString *mail = [email.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([self validarEmail:mail])
{
// code
}
-(BOOL) validarEmail:(NSString *) mail // From internet made for objective-c
{
BOOL stricterFilter = YES;
NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
If I comment the IF the app will be ok, if not, will crash.
Thanks in advance
Upvotes: 0
Views: 474
Reputation: 58478
You're attempting to test a regular expression on a UITextField
object when I assume you meant to test it on it's text
property instead.
Notice this line:
return [emailTest evaluateWithObject:email];
Your 'email' object is your text field, not your string. Your string is called mail
(it's passed into the method as a parameter.
Upvotes: 4
Reputation: 107231
Issue is with this line:
return [emailTest evaluateWithObject:email];
You need to change it to either:
return [emailTest evaluateWithObject:email.text];
or
return [emailTest evaluateWithObject:mail];
Upvotes: 1
Reputation: 685
You are trying to pass the UITextField object through to the regex, make sure that the object you are sending to the regex is an NSString otherwise you will have this error.
You should be using something like
NSString *myString = textField.text;
and then passing that NSString to the regex.
If you going to be using your regex function a lot you should probably do a check to make sure the variable you are passing is an NSString to prevent any exceptions in the future.
if ([myString isKindOfClass:[NSString class]]) { /* Code */ }
Upvotes: 1