【问题标题】:Objective C stringWithFormat if statementObjective C stringWithFormat if 语句
【发布时间】:2011-08-13 17:57:26
【问题描述】:

我对此很陌生 - 我正在尝试将 NSString stringWithFormat 与一些文本进行比较。它适用于 stringWithString。我无法弄清楚为什么它不适用于 stringWithFormat? 非常感谢任何人都可以提供的任何帮助!

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

【问题讨论】:

    标签: objective-c if-statement stringwithformat stringwithstring


    【解决方案1】:

    == 是一个相等的引用。要进行字符串比较,它必须是

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

    【讨论】:

    • 谢谢 - 在我发布后几秒钟就搞定了 - 在过去的 2 天里试图解决这个问题!
    【解决方案2】:

    应该是:

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

    【讨论】:

    • 另外,使用isEqualToString method,而不是==if ([theQuestion isEqualToString:@"The same thing"]){
    • StringWithFormat: @"The same thing" 应该没问题,虽然没用。我会改用stringWithString: @"The same thing"
    • …或者只是NSString *theQuestion = @"The same thing";
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 2015-03-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多