Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can combine <code>NSCharacterSet</code>s using a union to add more allowable characters: </p> <pre><code> NSMutableCharacterSet *myCharSet = [[NSCharacterSet alphanumericCharacterSet] mutableCopy]; [myCharSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]; </code></pre> <p>Here is the list of available character sets to choose from:</p> <pre><code>controlCharacterSet whitespaceCharacterSet whitespaceAndNewlineCharacterSet decimalDigitCharacterSet letterCharacterSet lowercaseLetterCharacterSet uppercaseLetterCharacterSet nonBaseCharacterSet alphanumericCharacterSet decomposableCharacterSet illegalCharacterSet punctuationCharacterSet capitalizedLetterCharacterSet symbolCharacterSet newlineCharacterSet </code></pre> <p>Also, your code is checking to make sure <strong>any</strong> character is not outside the set; I think your intention is to check that <strong>all</strong> characters are not outside the set. (This only applies when the user enters more than one character at once - think copy and paste).</p> <p>Consider adjusting your loop to look more like this:</p> <pre><code>NSCharacterSet *myCharSet = [NSCharacterSet alphanumericCharacterSet]; for (int i = 0; i &lt; [string length]; i++) { unichar c = [string characterAtIndex:i]; if ([myCharSet characterIsMember:c] == NO) { // add your user alert here return NO; } } </code></pre> <p>This way your loop won't simply exit on the first good character it finds; instead, it will exit on the first bad character.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload