Search code examples
iosutf-8nsstringfoundation

Bug in [NSString substringWithRange] if character would be UTF8 with 4 byte length?


I have to check each character in a given NSString for some condition.

When the string contains characters that would be represented in 4 bytes (in UTF-8 encoding) the result is not the character I expected.

NSString* oneCharString = [@"𝄞" substringWithRange:NSMakeRange(0, 1)];

The debugger shows:

enter image description here

It looks like an empty string. But the inspection shows some value (maybe a symbol for a non character ?).


Solution

  • If you want to get the first character of a string, you need to do it like this:

    NSString *str = ... // the string such as @"𝄞"
    NSRange firstRange = [str rangeOfComposedCharacterSequenceAtIndex:0];
    NSString *oneCharString = [str substringFromRange:firstRange];
    

    This code properly deals with composed characters.