我有这段代码:
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz1234567890"];
NSLog(@"character set: %i", [characterSet characterIsMember:(unichar)"a"]);
NSLog返回0,当我希望它返回1.我怎么做错了?
答案 0 :(得分:4)
您已将 C字符串(指针转换为char
)转换为unichar
。
将"a"
替换为'a'
会产生预期结果:
NSLog(@"character set: %i", [characterSet characterIsMember:(unichar)'a']);
答案 1 :(得分:0)
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz1234567890"];
unichar character = [@"cba" characterAtIndex:2];
NSLog(@"character set: %d", [characterSet characterIsMember:character]);
在这种情况下,转换不是问题, characterAtIndex 会返回正确输入的字符。