-2

What am I doing wrong with this conditional statement using a UILabel in Xcode?

Over simplified example, but I am trying to change the content of the UILabel in relation to text in the UILabel.

-(IBAction)answerQuestion:(id)sender{
    NSString *startingLableContent = @"Do dogs bark?";
    NSString *answer = @"Yes.";
    NSString *askAnother = @"What else do you want?";
    if (mainLable.text == startingLableContent) {
        mainLable.text = answer;
    }else if (mainLable.text == answer){
        mainLable.text = askAnother;
    }
}
jszumski
  • 7,430
  • 11
  • 40
  • 53
James
  • 7
  • 2

5 Answers5

1

You should compare them using isEqualToString

like this:

if ([mainLable.text isEqualToString:startingLableContent]) 

otherwise you are testing memory locations instead of content.

HalR
  • 11,411
  • 5
  • 48
  • 80
0

to compare Strings in Objective-C, use isEqualToString

-(IBAction)answerQuestion:(id)sender{
    NSString *startingLableContent = @"Do dogs bark?";
    NSString *answer = @"Yes.";
    NSString *askAnother = @"What else do you want?";
    if ([startingLableContent isEqualToString:mainLable.text]) {
        mainLable.text = answer;
    }else if ([answer isEqualToString:mainLable.text]){
        mainLable.text = askAnother;
    }
}
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
0

Try this:

if ([mainLable.text isEqualToString:startingLableContent]) {
// do stuff
}
sangony
  • 11,636
  • 4
  • 39
  • 55
0

==

It turns out that you can't just say if (someString == someOtherString). It will almost always return NO.

It has to do with some stuff about where the string is stored in memory. You can use == with int's and double's and those kind of things, but with NSString's, you need to do:

if ([mainLabel.text isEqualToString:someOtherString])

And so, your method should look like:

-(IBAction)answerQuestion:(id)sender{
    NSString *startingLableContent = @"Do dogs bark?";
    NSString *answer = @"Yes.";
    NSString *askAnother = @"What else do you want?";
    if ([mainLable.text isEqualToString:startingLableContent]) {
        mainLable.text = answer;
    }else if ([mainLable.text isEqualToString:answer]){
        mainLable.text = askAnother;
    }
}
Community
  • 1
  • 1
Undo
  • 25,519
  • 37
  • 106
  • 129
  • If they're both pointing to the same NSString object, `==` will return true. – Marcus Adams Apr 30 '13 at 19:34
  • @MarcusAdams And how often does that happen? If that happens in a real-world situation to you, I would suggest staying away from lightning storms for a while. Compilers will automatically point strings to other strings if it knows *at compile time* that it's the same string. Otherwise, well.... – Undo Apr 30 '13 at 19:36
  • You wouldn't be doing it if you knew at compile time that they were equal. This would be for run-time checking and there are plenty of reasons to do this. Hair's answer is correct and it was first. – Marcus Adams Apr 30 '13 at 19:45
0

You compare the pointers, not actual strings. If you want to compare strings, consider using isEqualToString instead.

tkroman
  • 4,811
  • 1
  • 26
  • 46