@class for typedef enum? @class for typedef enum? objective-c objective-c

@class for typedef enum?


No, there isn’t an equivalent.

Enumerations in Objective-C are the same as enumerations in C. Since the underlying type of an enumeration is implementation-dependent (e.g., it could be char or int), the compiler must know the complete declaration of the enumeration.

That said, a type specifier

enum nameOfEnum

without listing the enumeration members is valid C provided it appears after the type it specifies is complete, i.e., enum nameOfEnum { … } must appear beforehand in the translation unit.

In summary: There’s no forward declaration of enumerations, only backward references.


@Caleb, @Bavarious:

Most recent way (Jan, 2017) to forward declare the enum (NS_ENUM/NS_OPTION) in objective-c is to use the following:

// Forward declaration for XYZCharacterType in other header say XYZCharacter.htypedef NS_ENUM(NSUInteger, XYZCharacterType);// Enum declaration header: "XYZEnumType.h"#ifndef XYZCharacterType_h#define XYZCharacterType_htypedef NS_ENUM(NSUInteger, XYZEnumType) {    XYZCharacterTypeNotSet,    XYZCharacterTypeAgent,    XYZCharacterTypeKiller,};#endif /* XYZCharacterType_h */`

Similar question Forward-declare enum in Objective-C


Forward declaration of classes is necessary to enable two classes to refer to each other. It's not uncommon to have two classes that are defined in terms of each other:

@class ClassB;@interface ClassA : NSObject{    ClassB *objectB;}@end@interface ClassB : NSObject{    ClassA *objectA;}@end

There's no way to make that compile without the forward declaration.

The same is not true of enumerations. enum just creates a set of named values... you can't include one enumeration in the definition of another. Therefore, there's never a need to forward declare an enumeration.