Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off, your line:</p> <pre><code>NSString *firstLetter = [codeString substringFromIndex:1]; </code></pre> <p>does not get the first letter. This gives you a new string the contains all of the original string EXCEPT the first character. This is the opposite of what you want. You want:</p> <pre><code>NSString *firstLetter = [codeString substringToIndex:1]; </code></pre> <p>But there is a better way to see if the first character is a letter or not.</p> <pre><code>unichar firstChar = [[codeString uppercaseString] characterAtIndex:0]; if (firstChar &gt;= 'A' &amp;&amp; char &lt;= 'Z') { // The first character is a letter from A-Z or a-z } </code></pre> <p>However, since iOS apps deal with international users, it is far from ideal to simply look for the character being in the letters A-Z. A better approach would be:</p> <pre><code>unichar firstChar = [codeString characterAtIndex:0]; NSCharacterSet *letters = [NSCharacterSet letterCharacterSet]; if ([letters characterIsMember:firstChar]) { // The first character is a letter in some alphabet } </code></pre> <p>There are a few cases where this doesn't work as expected. <code>unichar</code> only holds 16-bit characters. But <code>NSString</code> values can actually have some 32-bit characters in them. Examples include many Emoji characters. So it's possible this code can give a false positive. Ideally you would want to do this:</p> <pre><code>NSRange first = [codeString rangeOfComposedCharacterSequenceAtIndex:0]; NSRange match = [codeString rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] options:0 range:first]; if (match.location != NSNotFound) { // codeString starts with a letter } </code></pre>
 

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