4

I'm pretty new to this - I'm trying to compare a NSString stringWithFormat with some text. It works fine with stringWithString. I can't work out why it doesn't work with stringWithFormat? Many thanks for any help anyone can offer!

    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);
    }
David
  • 43
  • 3

2 Answers2

5

== is a reference equal. to do a string compare it has to be the

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

}
jbtule
  • 31,383
  • 12
  • 95
  • 128
  • Thanks - figured it out seconds after I posted - spent the last 2 days trying to work this out! – David Aug 13 '11 at 18:16
0

It should be:

NSString *theQuestion = [NSString stringWithFormat:@"%@",@"The same thing"];
Saran
  • 6,274
  • 3
  • 39
  • 48