objective-c Objective C Unicode 字符比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/997776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:43:54  来源:igfitidea点击:

Objective C unicode character comparisons

objective-cunicode

提问by Alan

How are unicode comparisons coded? I need to test exactly as below, checking for specific letters in a string. The code below chokes: warning: comparison between pointer and integer

unicode 比较是如何编码的?我需要完全按照下面的方式进行测试,检查字符串中的特定字母。下面的代码窒息:警告:指针和整数之间的比较

for (charIndex = 0; charIndex < [myString length]; charIndex++)
{
   unichar testChar = [myString characterAtIndex:charIndex];

     if (testChar == "A")  
       // do something
     if (testChar == "B")
      // do something
     if (testChar == "C")
      // do something
}

回答by Jarret Hardie

For char literals, use single quotes:

对于字符文字,使用单引号:

if (testChar == 'A') NSLog(@"It's an A");

Or represent the character using the code point number:

或使用代码点编号表示字符:

if (testChar == 0x1e01) NSLog(@"It's an A with a ring below");

The compiler sees double-quotes as a string, so builds "A" as equivalent to a const char *(which gives you there error message about the pointer).

编译器将双引号视为字符串,因此将“A”构建为等效于 a const char *(它会为您提供有关指针的错误消息)。

回答by harveyswik

What are you really trying to do? Doing direct character comparisons is unusual. Typically -compare: or -isEqual: would be used to compare two strings. Or NSScanner would be used to analyze the components of a string.

你真正想要做什么?进行直接的字符比较是不寻常的。通常 -compare: 或 -isEqual: 将用于比较两个字符串。或者 NSScanner 将用于分析字符串的组成部分。