23

In one header file, I have something like:

// PasscodeInputViewController.h

typedef enum {
    PasscodeInputModeOn,     // set passcode
    PasscodeInputModeEnter,  // enter passcode
    PasscodeInputModeChange, // change passcode
    PasscodeInputModeOff     // turn off passcode
} PasscodeInputMode;

In another header file, I declare a method that takes an argument of type PasscodeInputMode:

#import "PasscodeInputViewController.h"

- (void)presentPasscodeInputWithMode:(PasscodeInputMode)mode;

As you can see, I use #import "PasscodeInputViewController.h" as above so that PasscodeInputMode is recognized, but is there a @class equivalent for typedef enum?

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • What do you mean? What specifically are you trying to do? – ughoavgfhw May 06 '11 at 03:46
  • what's the problem with the #import? – Inder Kumar Rathore May 06 '11 at 03:49
  • @Inder Kumar Rathore, well for class names you're supposed to use `@class` in the header file & `#import` in the .m file. I'm wondering if there's something analogous to `@class` for `typedef enum`s. – ma11hew28 May 06 '11 at 14:49
  • 1
    Swift adds a wrinkle. An include in a header may generate the error "include of non-modular header inside framework module". The solution is to move the header into the .m and use @class to forward declare anything missing. But for enums, there's apparently no way to do that. Suggestions? – David Gish Dec 19 '15 at 05:46
  • @ma11hew28, Forward declaring enums: https://stackoverflow.com/a/42073674/342794 – lal Sep 21 '17 at 17:41

4 Answers4

14

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.

5

@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.h
typedef NS_ENUM(NSUInteger, XYZCharacterType);


// Enum declaration header: "XYZEnumType.h"
#ifndef XYZCharacterType_h
#define XYZCharacterType_h

typedef NS_ENUM(NSUInteger, XYZEnumType) {
    XYZCharacterTypeNotSet,
    XYZCharacterTypeAgent,
    XYZCharacterTypeKiller,
};

#endif /* XYZCharacterType_h */`

Similar question Forward-declare enum in Objective-C

lal
  • 7,410
  • 7
  • 34
  • 45
0

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
-1

I think what you want is a class that has PasscodeInputMode as a property of it. That way you can be passing around an instantiated object of that class, and can set/get that property, and do other object-like-things with it (assume that's why you'd want to find a "@class equivalent"

Nektarios
  • 10,173
  • 8
  • 63
  • 93