9

I have a DataClass.h

@interface DataClass : NSObject
{
}

enum knownTypes
{
    type1 = 0,
    type2,
    type3,
    UnknownType = -1
};

Is there a way I can specify knownTypes in .m file and access from other class.

This is Util class i am creating, hence don't want to create an object to access the values in this class.

for ex: in TestClass.m , by importing DataClass.h , now i can use the enum values as type1,type2.. but if i declare the enum data in DataClass.m , i could not use those enum values.

1
  • Why put UnknownType at the bottom? Commented Apr 6, 2012 at 7:18

2 Answers 2

18

This has nothing to do with classes. This is a feature of C.

If you define a type or an enum in a .h file, you can use it by importing it (#import) where you need it.

If you define your enum in a .c or .m file, only elements after that definition in the file can use it.

In your case, it appears that you need the same enum in two different files. Usage is to define that enum in a separate file, e.g., knownTypes.h and import that file in the two files using it: DataClass.m and TestClass.m.

If TestClass is for testing purpose, then your current organization is OK: enum is declared in DataClass.h and both DataClass.m and TestClass.m import DataClass.h.

Sign up to request clarification or add additional context in comments.

Comments

2

No, if you define the enum in the source file instead of the header, then only that source file will be able to utilise the identifiers used in the enum. If you want to keep it “private” but usable by more than one source file, put it in a separate header and include this separate header in both source files.

1 Comment

Thats what I am trying to do, Data Class here is that separate Header file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.