2011-01-30 87 views
1

我目前正在在这样的字符串的一部分;撇号单字符比较问题

  if (thisChar == 'a') 
      [self a]; 

     else if (thisChar == 'b') 
      [self b]; 

     else if (thisChar == 'c') 
      [self c]; 

.........

 else if (thisChar == '0') 
     [self zero]; 

    else if (thisChar == '1') 
     [self one]; 

    else if (thisChar == '2') 
     [self two]; 

    else if (thisChar == '3') 
     [self three]; 

.........

  else if (thisChar == '.') 
     [self period]; 

    else if (thisChar == ',') 
     [self comma]; 

    else if (thisChar == '?') 
     [self qmark]; 

的问题,但出现时,我来到

else if (thisChar == ''') 
     [self three]; 

我要的是'(撇号)的迹象,但它不会让我这样做。我知道原因,但我不知道它的方式。这样做会有帮助吗?

NSString *apostropheString = [[NSString alloc] initWithFormat:@"'"]; 

unichar apostrophe = [apostropheString characterAtIndex:0]; 

我可以使用isEqualToString吗?

if ([apostrophe isEqualToString: thisChar]) 

任何帮助将不胜感激!


我找到了解决办法:

1)。创建与单个字符是一个撇号

NSString *apostropheString = [[NSString alloc] initWithFormat:@"'"]; 

2)NNString。得到它把它转换为一个名为“应用程序”单字符,因为它是零索引,并且只有一个字符的索引为0

unichar app = [apostropheString characterAtIndex:0]; 

3)。然后我说,如果“thisChar”等于“应用程序”运行“QMARK”

else if (thisChar == app) 
     [self qmark]; 

我希望这有助于为同样的问题任何人!

+0

别担心,我已经找到解决的办法!查看信息编辑。 – 2011-01-30 20:22:11

+2

你不能使用`'''`? – Gabe 2011-01-30 20:24:52

回答

3

您必须使用反斜线撇号:'\''