-3

Possible Duplicate:
Checking for equality in Objective-C

I have following codes:

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

And when I check the log it was displayed as following:

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

I was expecting:

Look, I've got the new title: test
Community
  • 1
  • 1
Daniel Chen
  • 1,933
  • 5
  • 24
  • 33

2 Answers2

2

inside the if statement, you need to use isEqualToString function:

if([self.itemTitleField.text isEqualToString:@"test"])
Dhruv Goel
  • 2,715
  • 1
  • 17
  • 17
  • 1
    Dhruv Goel is right. self.itemTitleField.text == @"test" compares the actual objects and so the newly created string @"test" is not the same object self.itemTitleField.text (you can print their memory location and you'll find that they are different). However, isEqualToString:@"test" compares the content of the string. – hishamaus Feb 04 '13 at 02:12
0

You shoud use isEqualToString instead of "==": if ([self.itemTitleField.text isEqualToString:@"test")) {

yebw
  • 247
  • 1
  • 3
  • 11