views:

374

answers:

2

I want to declare a custom enum such as:

enum menuItemType
{
    LinkInternal = 0,
    LinkExternal = 1,
    Image = 2,
    Movie = 3,
    MapQuery = 4
}

As a type for my object:

@interface MenuItem : NSObject {    
    NSMutableString *menuId;
    NSMutableString *title;
    enum menuItemType *menuType;
    NSMutableArray *subMenuItems;
}

But am unsure where I need to put the enum definition - if I put it before the @interface its syntactically incorrect

+3  A: 

If you put your two code snippets in a file in this order, you just have to add a ; at the end of the enum declaration and you may want to consider using an enum variable instead of a pointer to an enum variable.

This gives:

enum menuItemType
{
    LinkInternal = 0,
    LinkExternal = 1,
    Image = 2,
    Movie = 3,
    MapQuery = 4
};

@interface MenuItem : NSObject {    
    NSMutableString *menuId;
    NSMutableString *title;
    enum menuItemType menuType;
    NSMutableArray *subMenuItems;
}
mouviciel
thanks Im a bit of a obj-c noob so learning fast thanks
tigermain
+2  A: 

@mouviciel is right, but I thought I'd let you know that what you want is not a class property, something not supported in Objective-C. A class property is actually a global property that is set on the class-object. What you were thinking of was a plain-old property (set on an instance of the class).

Also, what your code shows is that you are just using an instance variable. Instance variables can be turned into properties by adding accessor/mutator methods as follows:

// after @interface {}
@property (readwrite) enum menuItemType menuType;

and

// under @implementation
@synthesize menuType;

This is a shortcut: the compiler will generate the proper methods to access and change the menuType property. I'm not sure how useful all this is to you, but it will help you with your understanding of Objective-C semantics.

Jonathan Sterling