Search code examples
objective-cif-statementstringwithformatstringwithstring

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 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);
    }

Solution

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

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