I have an enum typedef containing several type definitions, eg:
ActionTypeSomething = 1,
ActionTypeSomethingElse = 2
And so on.
So a method I’ve written evaluates a passed int and then returns a value (for example, a string) accordingly.
(NSString *)evaluatAndReturnProperResult:(int)typeID
NSString *repsonseString;
switch (typeID)
case actionTypeSomething: {
responseString = @"an appropriate string for typeID"
}
...
return responseString;
So my switch evaluates each supported type and returns the correct string.
Now for my question:
I only want to return strings for supported types (i.e., in theory any integer could be passed). If there’s no match, I return nil.
Obviously I can do this using exactly the method I already have. But could I (in theory) improve performance by evaluating the passed int to see if it matches any of my defined enum types BEFORE I send it through switch (the switch isn’t massive, but I’d still rather just return nil at the beginning of the method if I know there’s not going to be a match).
I’m sure this is easy, could someone suggest how to evaluate if my passed integer matches any define enum ActionType before I enter the switch? In this case I’m probably prematurely optimizing, but it’s more of a general question about how to do achieve it (not if I should).
The argument for your method shouldn’t be an int, it should ideally be the enum you have defined. This gives you some compile time checking.
If this is not possible then your default case in the switch will handle it just fine – that’s what they are designed for.