Stephen Touset Stephen Touset - 4 years ago 183
Objective-C Question

Forward-declare enum in Objective-C

I'm having trouble with enum visibility in an Objective-C program. I have two header files, and one defines a

typedef enum
. Another file needs to use the
typedef
'd type.

In straight C, I would simply
#include
the other header file, but in Objective-C, it's recommended not to use
#import
between header files, instead using forward
@class
declarations as needed. However, I can't figure out how to forward-declare an enumeration type.

I don't need the actual enumerated values, except in the corresponding
.m
implementation file, where I can safely
#import
away. So how can I get the
typedef enum
to be recognized in the header?

Answer Source

Go ahead and use #import. The only reason people recommend to use @class when possible is because it makes your code slightly faster to compile. However, there is no issue with #importing one .h file from another. In fact, you need to do this when extending another class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download