Is it possible to scope enums to a class in Objective-C?
enums, objective-c
Solution
You have to prefix your public enums. Simply put the enum definition in the header of your class.
// ClassA.h
typedef enum {
ClassAStatusAccepted,
ClassAStatusRejected
} ClassAStatus;
@interface ClassA {
ClassAStatus status;
}
@end
// ClassB.h
typedef enum {
ClassBStatusAccepted,
ClassBStatusRejected
} ClassBStatus;
@interface ClassB {
ClassBStatus status;
}
@end
This is how Apple does it.
Or you could use the new style:
// UIView.h
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
UIViewAnimationCurveEaseInOut, // slow at beginning and end
UIViewAnimationCurveEaseIn, // slow at beginning
UIViewAnimationCurveEaseOut, // slow at end
UIViewAnimationCurveLinear
};
Problem
I'm new to Objective-C and trying to figure out enums. Is there a way to scope an enum to a class so that the values could be used by another class? Something like this: ``` @interface ClassA { typedef enum { ACCEPTED, REJECTED } ClassAStatus; } @end @interface ClassB { typedef enum { ACCEPTED, REJECTED } ClassBStatus; } @end ``` Though that does not work, obviously. Or is there a better way to do enums altogether? Edit: I guess my wording wasn't clear, but I'm not asking how to declare enums. I'm aware that putting them at the top of the file works. I'm asking if there's a way to scope them so the values aren't global to the entire file.