-2

Possible Duplicate:
Objective-C constants: NSString comparison using ==?

i have a textfiela nd label and want the label to display certain text when text is in the textfield here is what i have so far

- (IBAction)Button {
if (Textfield1.text = @"A") {
    int text = arc4random() % 3;
    switch (text) {
        case 0:
            Label1.text = @"Red";
            break;
        case 1:
            Label1.text = @"Blue";
            break;
        case 2:
            Label1.text = @"Green";
            break;
        case 3:
            Label1.text = @"Yellow";
            break;

        default:
            break;
    }
}

}

all that happens is if prints the letter a in the textfield

any ideas?

Community
  • 1
  • 1
Seniac
  • 3
  • 1

3 Answers3

2

You shouldn't compare directly with == between NSObjects (and currently you're assigning and not comparing, by using one equal-sign instead of two). Instead try the following:

if ([Textfield1.text isEqualToString:@"A"]) {
johankj
  • 1,765
  • 16
  • 34
  • @Seniac: The reason to use isEqualToString: (and its related method isEqual:) is [answered in this question](http://stackoverflow.com/questions/8979408/objective-c-constants-nsstring-comparison-using) (as H2CO3 also links to). If this does answer your question, then please upvote and mark the answer as accepted. Merry Christmas (or Happy Holiday if you want to be political correct) :) – johankj Dec 24 '12 at 21:55
0

You need to use isEqualToString

For example: if ([[label text] isEqualToString:@"someString"]) { // whatever }

mdominick
  • 1,319
  • 1
  • 10
  • 21
0

USE:

if([Textfield1.text isEqualToString:@"A")
Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41