Alan
Alan

Reputation: 1265

Objective C unicode character comparisons

How are unicode comparisons coded? I need to test exactly as below, checking for specific letters in a string. The code below chokes: warning: comparison between pointer and integer

for (charIndex = 0; charIndex < [myString length]; charIndex++)
{
   unichar testChar = [myString characterAtIndex:charIndex];

     if (testChar == "A")  
       // do something
     if (testChar == "B")
      // do something
     if (testChar == "C")
      // do something
}

Upvotes: 16

Views: 21980

Answers (2)

harveyswik
harveyswik

Reputation: 19

What are you really trying to do? Doing direct character comparisons is unusual. Typically -compare: or -isEqual: would be used to compare two strings. Or NSScanner would be used to analyze the components of a string.

Upvotes: 0

Jarret Hardie
Jarret Hardie

Reputation: 98002

For char literals, use single quotes:

if (testChar == 'A') NSLog(@"It's an A");

Or represent the character using the code point number:

if (testChar == 0x1e01) NSLog(@"It's an A with a ring below");

The compiler sees double-quotes as a string, so builds "A" as equivalent to a const char * (which gives you there error message about the pointer).

Upvotes: 24

Related Questions