简体   繁体   English

如何比较UITextfield输入与不同的字符串?

[英]How do I compare UITextfield input to different strings?

I want to compare the value of the input from a UITextfield to some other strings. 我想将UITextfield的输入值与其他一些字符串进行比较。 Here's my guessing. 这是我的猜测。

if ([textfield.text isEqualToString:@"First" || @"Second" || @"Third"]) {
    // do something
}

Is there any better approach to this? 有什么更好的办法吗?

Put the ors in the right place: 将ors放在正确的位置:

if([textfield.text isEqualToString:@"First"] || 
   [textfield.text isEqualToString:@"Second"] || 
   [textfield.text isEqualToString:@"Third"]) 

In a situation where you have a series of objects such as your example, you would add then to an array and test its existence in the array: 在有一系列对象(例如您的示例)的情况下,应将其添加到数组中并测试其在数组中的存在:

Example

NSMutableArray *arr = [[[NSMutableArray alloc] init] autorelease];
[arr addObject:@"First"];
[arr addObject:@"Second"];
[arr addObject:@"Third"];

if ([arr containsObject:textField.text])
{
    // do something
}

Add First, Secound and Third to an array and then 将First,Secound和Third添加到数组,然后

if([myarray containsObject:someObject]){
// I contain the object
}

This approach saves you time and code ;) 这种方法可以节省您的时间和代码;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM