Цель C stringWithFormat if - PullRequest
       9

Цель C stringWithFormat if

4 голосов
/ 13 августа 2011

Я довольно новичок в этом - я пытаюсь сравнить строку NSStringWithFormat с некоторым текстом. Он отлично работает с 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);
    }

Ответы [ 2 ]

5 голосов
/ 13 августа 2011

== является ссылкой равной. для сравнения строк это должно быть

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

}
0 голосов
/ 13 августа 2011

Должно быть:

NSString *theQuestion = [NSString stringWithFormat:@"%@",@"The same thing"];
...