Search code examples
ioscocoa-touchnsstringuikeyinput

Determining ascii character in NSString


In one of my UIView classes, I have the UIKeyInput protocol attached to gather input from a UIKeyboard. I'm trying to figure out what ascii character is being used when the space button is pushed (it's not simply ' ' it's something else it appears). Does anyone know what this asci character is or how I can figure out what ascii code is being used?


Solution

  • To look at the value for each character you can do something like this:

    NSString *text = ... // the text to examine
    for (NSUInteger c = 0; c < text.length; c++) {
        unichar char = [text characterAtIndex:c];
        NSLog(@"char = %x", (int)char); // Log the hex value of the Unicode character
    }
    

    Please note that this code doesn't properly handle any Unicode characters in the range \U10000 and up. This includes many (all?) of the Emoji characters.