Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New modern Objective-C enum type check

If I declare enum type with new NS_ENUM macro which was introduced because of stronger type check, am I able to check for this type also in runtime?

I mean, I have

typedef NS_ENUM(NSUInteger, MyNewType) {

    MyNewTypeInstance1,
    MyNewTypeInstance2,
    MyNewTypeInstance3

};

. And I want to know that for example (NSUInteger)i = 2 is kind of MyNewType.

like image 536
user500 Avatar asked Nov 20 '12 15:11

user500


1 Answers

No. NS_ENUM is just a way of using a feature introduced to Objective-C via C++11 called "fixed underlying types" for enumerations. This ensures that the type used to store the enumerated values is of a fixed size and signedness, but it doesn't allow you to inquire about the enumerated type at runtime.

If you're interested in validating whether values are actually members of your enumeration, there are two related approaches for that. If the values are contiguous, you can write a macro that checks whether the value in question is in the valid contiguous range. Otherwise, you can take the more general (and verbose) approach that Apple takes with, e.g. UIDeviceOrientationIsValidInterfaceOrientation, and explicitly check against all valid enumerated values.

like image 104
warrenm Avatar answered Sep 19 '22 12:09

warrenm