mstottrop
mstottrop

Reputation: 589

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. Here's my guessing.

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

Is there any better approach to this?

Upvotes: 0

Views: 1445

Answers (3)

Jeremy
Jeremy

Reputation: 9010

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
}

Upvotes: 2

greenLizard
greenLizard

Reputation: 2346

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 ;)

Upvotes: 0

PengOne
PengOne

Reputation: 48398

Put the ors in the right place:

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

Upvotes: 4

Related Questions