我有这个代码:
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz1234567890"];
NSLog(@"character set: %i", [characterSet characterIsMember:(unichar)"a"]);
当我期望它返回 1 时,NSLog 返回 0。我怎么做错了?
我有这个代码:
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz1234567890"];
NSLog(@"character set: %i", [characterSet characterIsMember:(unichar)"a"]);
当我期望它返回 1 时,NSLog 返回 0。我怎么做错了?
您已将C 字符串(指向 的指针char
)转换为unichar
. 替换"a"
为'a'
产生预期的结果:
NSLog(@"character set: %i", [characterSet characterIsMember:(unichar)'a']);
马丁完全正确;当您从 string 读取 unichar 时,现实生活中的情况是什么:
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz1234567890"];
unichar character = [@"cba" characterAtIndex:2];
NSLog(@"character set: %d", [characterSet characterIsMember:character]);
在这种情况下,转换不是问题,characterAtIndex返回正确键入的字符。