1

Is there any way to assign a set of values to an enum and check wether a variable has its value in that enum in objective-c. Something like getting the index of an object in an array if the object is present in an array. I want to give a common behavior, when certain indexes(may not be in order) tapped in my table view, want to avoid if-ing all the indexes, hope I can use enum style.

3 Answers 3

2

Objective-C, like most programming languages, doesn't provide a "type membership" test for primitive types - just for object types (e.g. isKindOfClass:) - so using an enum won't solve your problem.

However given that you are thinking of an enum in the first place it sounds like you have a small fixed set of values you wish to check for. In that case you can just use a static array, e.g. something like:

static NSUInteger indices[] = { 2, 42, 57 }; 

The number of values in such an array is given by sizeof(indices) / sizeof(NSUInteger and you can write a simple loop or binary search to test for membership.

If you wish to use objects then NSSet is a good choice, if the numbers will vary during execution then NSMutableSet. E.g.

NSSet *indices = [NSSet setWithObjects:@2, @42, @57, nil]; 

and you test for membership using member::

if ([indices member:@indexToTest] != nil) ... 

(Note: unlike NSArray the specification for containsObject: for NSSet does not say it uses isEqual:, hence the use of member: here.)

Sign up to request clarification or add additional context in comments.

1 Comment

Helpful answer, i think i should create a set of indexpaths and check if the current selected indexpath belongs to the set and perform suitable actions. Thanks :)
1

You can't use an enum for that, because you can't enumerate them or call sth. like contains on them. If I understand your correctly I think the straight forward way to do that would be to just use an NSArray to store the indices you care about. Say you want to recognize the indices 2,6 and 14:

NSArray *indices = [NSArray arrayWithObjects: @2, @6, @14, nil]; 

(The @ is necessary because an NSArray can only store objects. The @ turns the integers into NSNumber instances.

Then to check if the currently selected index is in it you use:

int indexToCheck = ...; BOOL indexIsInArray = [index containsObject:@(indexToCheck)]; 

If your looking for a more 'fancy' (and faster) way to that, you could use bit operations (again using 2,6 and 14 as example):

int indices = 1 << 2 | 1 << 6 | 1 << 14; 

and then to check:

int indexToCheck = ...; BOOL isValid = (1 << indexToCheck) & indices; 

where isValid is true when the indexToCheck in one of your indices.

1 Comment

easy mistake to make given the name, but containsObject: does a value comparison by calling isEqual: and does not do reference comparison. So if using an array it is just [indices containsObject:@(indexToCheck)]
0

No, internally enums are based on integers, and the mapping happens at compile time. So you need a specific check for every enum type (if they are consecutive numbers, a ">" and "<" check will do).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.