Objective C stringWithFormat if statement

I'm new to this - I'm trying to compare an NSString stringWithFormat with some text. It works great with stringWithString. I can not understand why it does not work with stringWithFormat? Thanks so much 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); } 
+4
source share
2 answers

== is a reference. to compare strings it should be

 if([theQuestion isEqualToString:@"The same thing"]){ } 
+5
source

It should be:

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

All Articles