开发者

Recognizing multiple string values in a single action

Thanks again for the help.

I have a simple action that checks the stringValue of a textField, and if it matches - a status message prints in a second textField:

 if 

 开发者_运维知识库(textField.stringValue == (@"Whatever" )){
 [outputDisplay setStringValue:@"Success"];

My question is how do I implement multiple input stringValue options in this method? For example "Whatever" "Whatever1, Whatever2" all return "Success" in the outputDisplay.

thanks.

Paul


Create a set of answers you're looking for and test if the string in question is in there.

NSSet *successStrings = [NSSet setWithObjects:@"Whatever1",
                                              @"Whatever2",
                                              @"Whatever3",
                                              nil];
if ([successStrings containsObject:st]) {
    [outputDisplay setStringValue:@"Success"];
}

(An array would also work, but a set is specialized for testing membership, so it's a better fit for what we're doing here.)


Firstly, to check for equality of NSString-s you should use -isEqualToString:. == compares the pointer values which often returns NO even if the two strings' contents are the same.

To check if the text field match multiple strings, you connect them with the || (or) operator, so you get

NSString* st = textField.stringValue;
if ([st isEqualToString:@"Whatever"] || [st isEqualToString:@"Whatever1"] || [st isEqualToString:@"Whatever2"]) {
  [outputDisplay setStringValue:@"Success"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜