Josh Sherick
Josh Sherick

Reputation: 2161

typedef enum: which classes have access to it

NOTE: i changed the names, i dont actually have a type letters.

in my root view controller .h, i do

typedef enum {
a,
b,
c,
d,
e }letters;

i have several other classes, most of which need to use this "letters" type. in my root view controller, i use "#import "MyClass.h" to import the class (since i make an instance of it in rootViewController). however, this does not allow MyClass to use the type "letters".

i tried adding in MyClass.h, "#import rootViewController.h", but xcode started giving me errors (only sometimes, it was on and off). what is the best way to do this? should i just import the rootViewController to all of the classes that need this "letters" type? is it ok to have both rootViewController importing MyClass and MyClass importing rootViewController?

Upvotes: 5

Views: 5593

Answers (2)

Anomie
Anomie

Reputation: 94794

If you need to use the enum only in connection with one class, go ahead and include it in that class's .h file. If you need to use the enum in many different places, you would probably be better served by creating a .h file just to declare the enum, and include that wherever you need it.

Your errors were probably coming from missing forward declarations. If RootViewController has ivars, properties, or method signatures that refer to MyClass, then there needs to be either a @class MyClass; or an @interface block for MyClass visible before the compiler gets to those ivars, properties, or method signatures. If RootViewController.h and MyClass.h both include the other, then it will work if RootViewController.h is imported first but fail if MyClass.h is imported first.

Upvotes: 11

bbum
bbum

Reputation: 162712

A typedef enum is kinda like a #define with a bit of error checking (limited to integral types).

I.e. about the only way it can cause a problem is if you screw up the syntax or declare something with the same name (a re-declaration error).

Outside of that, unlikely to be your problem.

Impossible to say more without seeing the error messages.

Upvotes: 0

Related Questions