-2

可能重复:
Objective-C 常量:NSString 比较使用 ==?

我有一个 textfiela nd 标签,希望标签在文本字段中显示某些文本时,这是我目前所拥有的

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

}

所发生的只是如果在文本字段中打印字母 a

有任何想法吗?

4

3 回答 3

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"]) {
于 2012-12-24T21:46:00.320 回答
0

You need to use isEqualToString

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

于 2012-12-24T21:45:43.127 回答
0

USE:

if([Textfield1.text isEqualToString:@"A")
于 2012-12-24T21:45:59.587 回答