You want to look at string comparison methods on the NSString class. The simplest example is to use isEqualToString:
, as such:
if([textName.text isEqualToString:@"blue"]) {
text = @"Wrong color";
}
However, that can be somewhat limiting, as you're looking specifically if the word is "blue", all in lowercase. If you want to accept different combinations of cases (so that, for example, "blue", "Blue", and "BLUE" all cause "Wrong color"), then you want to use caseInsensitiveCompare:
, like this:
if([textName.text caseInsensitiveCompare:@"blue"] == NSOrderedSame) {
text = @"Wrong color";
}
The big difference is that isEqualToString:
will return a boolean value, so you can test it directly inside your if
, but caseInsensitiveCompare:
returns an NSComparisonResult
, so you have to check if it's NSOrderedSame
rather than testing the return value itself.
For more info: