为什么我的条件走错了路

时间:2013-02-04 02:08:00

标签: ios objective-c xcode

  

可能重复:
  Checking for equality in Objective-C

我有以下代码:

    NSLog(@"Before, let's see what is in itemTitleField: %@", self.itemTitleField.text);
    NSLog(@"And the item title was: %@",self.item.title);
    if (self.itemTitleField.text == @"test") {
        NSLog(@"Look, I've got the new title: %@",self.itemTitleField.text);
    } else {
        NSLog(@"No the item title was different: %@", self.itemTitleField.text);
    }

当我检查日志时,它显示如下:

2013-02-04 09:59:03.308 Test[1275:11303] Before, let's see what is in itemTitleField: test
2013-02-04 09:59:03.309 Test[1275:11303] And the item title was: test
2013-02-04 09:59:03.309 Test[1275:11303] No the item title was different: test

我在期待:

Look, I've got the new title: test

2 个答案:

答案 0 :(得分:2)

在if语句中,你需要使用isEqualToString函数:

if([self.itemTitleField.text isEqualToString:@"test"])

答案 1 :(得分:0)

你应该使用isEqualToString而不是“==”:if([self.itemTitleField.text isEqualToString:@“test”)){{/ p>