Search code examples
objective-cnscharacterset

CharacterInString not producing expected result


I have this code:

NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz1234567890"];

NSLog(@"character set: %i", [characterSet characterIsMember:(unichar)"a"]);

The NSLog returns 0, when I expect it to return 1. How am i doing this wrong?


Solution

  • You have cast a C string (a pointer to char) to unichar. Replacing "a" by 'a' produces the expected result:

    NSLog(@"character set: %i", [characterSet characterIsMember:(unichar)'a']);