Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using enum datatype declared in another class in Objective C

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.

like image 734
Friendtam Avatar asked Apr 06 '12 07:04

Friendtam


People also ask

What is typedef enum in Objective C?

A typedef in Objective-C is exactly the same as a typedef in C. And an enum in Objective-C is exactly the same as an enum in C. This declares an enum with three constants kCircle = 0, kRectangle = 1 and kOblateSpheroid = 2, and gives the enum type the name ShapeType.

Can we have enum inside enum in C?

It is not possible to have enum of enums, but you could represent your data by having the type and cause separately either as part of a struct or allocating certain bits for each of the fields.

Can enum values be changed in C?

You can change default values of enum elements during declaration (if necessary).


1 Answers

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.

like image 103
mouviciel Avatar answered Sep 28 '22 07:09

mouviciel