Validate NSString

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-06T18:50:49Z Indexed on 2010/04/06 18:53 UTC
Read the original article Hit count: 117

Filed under:
|
|

I am validating an NSString to ensure that the string does not contain apostrophes.

The code I'm using to do this is

NSCharacterSet * invalidNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"'"];
NSScanner * scanner = [NSScanner scannerWithString:string];
NSString  * scannerResult;
[scanner setCharactersToBeSkipped:nil];
[scanner scanUpToCharactersFromSet:invalidNumberSet intoString:&scannerResult];
if(![string isEqualToString:scannerResult])
{
    return 2;
}

Returning 2 represents an error. This code works, except for the case where the string is an apostrophe.

To get around this issue, I added the following code above the preceding block.

if([string isEqualToString:@"'"]);
{
    return 2;
}

This code is evaluating to true, regardless of the input. I need to either prevent the first block from crashing with the input of ', or get the second block to work.

What am I missing?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c