10

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?

QuickNick
  • 1,921
  • 2
  • 15
  • 30

3 Answers3

11

.h

typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;

@interface MyFirstClass : NSObject {

 MyTypes type;

 }

.m file

   type=VALUE_A;
  • In 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). – superpuccio Dec 15 '14 at 17:18
  • If 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! – Vijay-Apple-Dev.blogspot.com Dec 16 '14 at 06:04
7

Outside of the @interface declaration.

typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;

@interface MyFirstClass : NSObject {
}

@end
JeremyP
  • 84,577
  • 15
  • 123
  • 161
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;
Rajan Twanabashu
  • 4,586
  • 5
  • 43
  • 55