Madivad
Madivad

Reputation: 3337

test if object is in an array of objects

I know I'm overlooking something easy, but I am using

if (textField == self.nameTextField || textField == self.ageTextField || textField == ...)

when ideally I'd like to use something that allows the textField to be compared to an array of allowed (named) textField objects.

I've tried to pseudocode what I think the solution would look like a couple of times, I just don't know what I'm doing. Can anyone point me in the right direction?

Upvotes: 0

Views: 172

Answers (2)

Paresh Navadiya
Paresh Navadiya

Reputation: 38249

Firstly add all references of UITextFields in NSArray.

Now use containsObject in NSArray to check if exists

BOOL contains = [yourArrayofTextFields containsObject:textField];
if(contains)
  // contains
else
  // not contains

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107131

Just check like:

if([textFieldArray containsObject:textField])
{
     //do stuff here
}

Here textFieldArray is the array which holds all textFields.

If the textField object is in the textFieldArray, it'll return true.

Upvotes: 1

Related Questions