How do I compare UITextfield input to different strings?

≯℡__Kan透↙ 提交于 2019-12-12 03:04:15

问题


I want to compare the value of the input from a UITextfield to some other strings. Here's my guessing.

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

Is there any better approach to this?


回答1:


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
}



回答2:


Put the ors in the right place:

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



回答3:


Add First, Secound and Third to an array and then

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

This approach saves you time and code ;)



来源:https://stackoverflow.com/questions/8717789/how-do-i-compare-uitextfield-input-to-different-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!