I would like to use something like:
if (foo>0){
typedef enum {
Form_FirstName = 0,
Form_NamePrefix,
Form_LastName,
Form_Email,
Form_Phone
} Form;
} else {
typedef enum {
Form_FirstName = 0,
Form_LastName,
Form_Phone
} Form;
}
Can this be done? Where should I do this? In the .m or in the .h? I want to use this for an UITableView.
No.
Firstly, enumerations are compile type constructs.
Secondly, you have a scoping issue. Because you are defining the enumeration type within the scope of the
ifand theelse. It will not have visibility outside of theif..else..statement.You’ll need to find a different way to differentiate your indexing based on a state.
Updating based on OP’s follow on question:
OK, you need a map of some sort. For example you could do:
Define your enumeration.
Assuming your class has an
indexesivar with the usual@propertyand@synthesize, set up your indexes:Elsewhere, when you need to translate an enumerated field to an index: