4

我对此很陌生-我正在尝试将 NSString stringWithFormat 与一些文本进行比较。它适用于 stringWithString。我无法弄清楚为什么它不适用于 stringWithFormat?非常感谢任何人都可以提供的任何帮助!

    NSString *theQuestion = [NSString stringWithFormat:@"The same thing"];
if (theQuestion == @"The same thing"){
        NSLog(@"stringWithFormat is the same as the given text");
    }else{
NSLog(@"stringWithFormat is NOT the same as the given text");
        NSLog(@"but theQuestion is:\"%@\"", theQuestion);
    }
4

2 回答 2

5

==是参考相等。要进行字符串比较,它必须是

if([theQuestion isEqualToString:@"The same thing"]){

}
于 2011-08-13T18:01:33.167 回答
0

它应该是:

NSString *theQuestion = [NSString stringWithFormat:@"%@",@"The same thing"];
于 2011-08-13T18:00:45.227 回答