1

In Xcode I'm trying to get the text of an NSTextField (Label) to see if it says Yes or it is says No

I've tried this:

if ([LabelYesNo StringValue] == @"Yes"){
    [LabelYesNo setStringValue:@"No"];
else{
    [LabelYesNo setStringValue:@"Yes"];
    }
}

and

if (LabelYesNo isEqualToString @"Yes"){
    [LabelYesNo setStringValue:@"No"];
else{
    [LabelYesNo setStringValue:@"Yes"];
    }
}

and a few other variations of that. Just can't seem to get it right.... Can anyone help?

Thanks

Nekto
  • 17,837
  • 1
  • 55
  • 65
Wiggonator
  • 37
  • 6

1 Answers1

2
[[theTextField stringValue] isEqualToString:@"Yes"];

should work

in your first code, you're comparing strings via ==. Using the C == operator will simply compare the addresses of the objects.

in your second code, your whole code is wrong, and you'are trying to compare element of type NSTextField to NSString.

see String comparison in Objective-C

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
  • Marek, Brilliant - thanks for the answer. I completely understand why it needs to be this way now that you've written it out. – Wiggonator Sep 25 '11 at 18:49