为什么字符串不相等什么存储? [英] Why Does String Not Equal What Is Stored?

查看:81
本文介绍了为什么字符串不相等什么存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单而奇怪的问题...

This is a simple, odd question...

if(tableViewNum == @"One") {
        if ([drinkArray objectAtIndex:0] == currentDate) {
            [updatedArray addObject:drinkArray];
            NSLog(@"MADE THE ELSE1");
        }
        NSLog(@"MADE THE ELSE2");
    }
    else if (tableViewNum == @"Two") {
        if ([[drinkArray objectAtIndex:0] isEqualToString:yesterdayDate])
            [updatedArray addObject:drinkArray];
    } else {
        NSLog(@"MADE THE ELSE %@",tableViewNum);
        [updatedArray addObject:drinkArray];
    }

在第一个if语句中,我问tableViewNum == @One

In the very first if statement I ask if tableViewNum == @"One"

但即使tableViewNum确实等于@One,我也不会进入if语句的那一部分

But I don't go in that section of the if statement even though tableViewNum actually does equal @"One"

正如你所看到的那样,最后一个NSLog的出现方式是

As you can see the very last NSLog all ways comes out as

MADE THE ELSE One

但是如果tableViewNum真的等于One它会通过if语句而不是else语句代码... ?????

But if tableViewNum really equaled One it would have gone through the if statement not the else statement code...?????

推荐答案

您无法将字符串与 == 运算符进行比较。改为使用 isEqualToString

You can't compare strings with the == operator. Use isEqualToString instead:

if([tableViewNum isEqualToString:@"One"]) {
    // etc.

…其他条件也一样。你已经在第二个区块中正确地做了。

… and the same for the rest of the conditions. You're already doing it right in the second block.

这篇关于为什么字符串不相等什么存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆