0

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?

HAS
  • 19,140
  • 6
  • 31
  • 53
Andrew
  • 15,935
  • 28
  • 121
  • 203

2 Answers2

4

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']);
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

Martin is fully true; what can be a real-life situation when you read your unichar from string :

NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz1234567890"];
unichar character = [@"cba" characterAtIndex:2];
NSLog(@"character set: %d", [characterSet characterIsMember:character]);

In that case casting is not an issue, characterAtIndex returns the properly typed char.

nzs
  • 3,252
  • 17
  • 22