如果“word”文本会说“错误”(iPhone SDK)

时间:2021-09-21 10:44:35

I would like to change this code so that if the textName.text contains the word "Blue" it will say @"Wrong color"

我想更改此代码,以便如果textName.text包含单词“Blue”,则会显示@“Wrong color”

This code implies only that if the word has no letters it will say "Wrong color". How can I change it to a specific word (blue) instead of "length] == 0)"?

此代码仅暗示如果单词没有字母,则表示“颜色错误”。如何将其更改为特定单词(蓝色)而不是“length”== 0)“?

if([textName.text length] == 0)
{
    text = @"Wrong color";
}

Thank you very much!

非常感谢你!

2 个解决方案

#1


You want to look at string comparison methods on the NSString class. The simplest example is to use isEqualToString:, as such:

您想要查看NSString类上的字符串比较方法。最简单的例子是使用isEqualToString:,如下:

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:

但是,这可能会有所限制,因为您正在具体查看单词是否为“blue”,全部为小写。如果你想接受不同的案例组合(例如,“蓝色”,“蓝色”和“蓝色”都会导致“颜色错误”),那么你想使用caseInsensitiveCompare :,像这样:

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.

最大的区别是isEqualToString:会返回一个布尔值,所以你可以在if中直接测试它,但是caseInsensitiveCompare:返回一个NSComparisonResult,所以你必须检查它是否是NSOrderedSame而不是测试返回值本身。

For more info:

欲了解更多信息:

#2


if([textName.text isEqualToString:@"blue"]) {
        text = @"Wrong color";
}

#1


You want to look at string comparison methods on the NSString class. The simplest example is to use isEqualToString:, as such:

您想要查看NSString类上的字符串比较方法。最简单的例子是使用isEqualToString:,如下:

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:

但是,这可能会有所限制,因为您正在具体查看单词是否为“blue”,全部为小写。如果你想接受不同的案例组合(例如,“蓝色”,“蓝色”和“蓝色”都会导致“颜色错误”),那么你想使用caseInsensitiveCompare :,像这样:

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.

最大的区别是isEqualToString:会返回一个布尔值,所以你可以在if中直接测试它,但是caseInsensitiveCompare:返回一个NSComparisonResult,所以你必须检查它是否是NSOrderedSame而不是测试返回值本身。

For more info:

欲了解更多信息:

#2


if([textName.text isEqualToString:@"blue"]) {
        text = @"Wrong color";
}