Objective C stringWithFormat if statement
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 ca开发者_JAVA技巧n 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);
}
==
is a reference equal. to do a string compare it has to be the
if([theQuestion isEqualToString:@"The same thing"]){
}
It should be:
NSString *theQuestion = [NSString stringWithFormat:@"%@",@"The same thing"];
精彩评论