开发者

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"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜