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.
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.
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.
You can change default values of enum elements during declaration (if necessary).
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With