Objective-C: where and how should I declare enums?
10,621
Solution 1
.h
typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;
@interface MyFirstClass : NSObject {
MyTypes type;
}
.m file
type=VALUE_A;
Solution 2
Outside of the @interface
declaration.
typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;
@interface MyFirstClass : NSObject {
}
@end
Solution 3
You can create a header file (*.h) and do following to match your enum variable.
// EnumConstants.h
#ifndef EnumConstants_h
#define EnumConstants_h
typedef enum {
VEHICLE,
USERNAME
} EDIT_TYPE;
typedef enum {
HIGH_FLOW,
STANDARD_FLOW
} FLOW_TYPE;
#endif
Uses:
#import "EnumConstants.h"
UISwitch *onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(self.tableview.frame.size.width-75, 26, 0, 0)];
onOffSwitch.tag =STANDARD_FLOW;
Author by
QuickNick
I'm QuickNick from Russia. I collect programming languages. @None
Updated on June 18, 2022Comments
-
QuickNick almost 2 years
Good day, friends. I'm newbie in Objective-C. I'm wanting to use enum in my class and make it public. I've understand how to declare enums (http://stackoverflow.com/questions/1662183/using-enum-in-objective-c), but I don't understand where should I declare them.
I've tried:
@interface MyFirstClass : NSObject { typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes; }
or:
@interface MyFirstClass : NSObject { @public typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes; }
But compiler throws error: "expected specifier-qualifier-list before typedef".
What's wrong?
-
superpuccio over 9 yearsIn this case what is the visibility of the enum? (I'm not speaking of the "type" variable, I'm speaking of the enum outside the interface).
-
Vijay-Apple-Dev.blogspot.com over 9 yearsIf you want to use this myTypes enum somewhere in your code on other class, then you have to import the MyFirstClass.h in that class directly or indirectly. So the visibility of enum is, where it was defined. If u want to use the enum in all the class that u have then just create the seperate EnumConstants.h file put them all there, import them in YourProject-Prefix.pch. So it will be visible to all your classes.I hope it will help out somebody. Thanks!
-
NYC Tech Engineer about 7 yearsWhat is your macro doing?