Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an enum value as being deprecated in ObjectiveC (2.0)

Suppose a long time ago, I had created the following enumeration:

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricSquare,
   GeometricRectangle,
   GeometricCircle
}GeometricFigures;

I introduced those a while ago within my awesome engine and now I have finally decided that people should not use GeometricSquare anymore as that is covered by GeometricRectangle already.

For a start, I would possibly change my enumeration towards something like this:

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricRectangle,
   GeometricSquare = GeometricRectangle,
   GeometricCircle
}GeometricFigures;

That would certainly keep my awesome engine backwards compatible but on the other hand increase the legacy junk. Hence I would like to remove GeometricSquare altogether in a foreseeable future. To make that obvious to the users of my engine, I would like to mark GeometricSquare as being deprecated.

My goal is that the documentation (doxygen) as well as the code completion (Xcode) and last but not least the compiler (GCC) will make it obvious to the user that GeometricSquare should not be used anymore and has been replaced by GeometricRectangle.

For the documentation, I would simply use @deprecated keyword;

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricRectangle,
   ///@deprecated Has been replaced by GeometricRectangle
   GeometricSquare = GeometricRectangle,
   GeometricCircle
}GeometricFigures;

But how about Xcode and GCC?

Unfortunately, the usual GCC (method) attribute does not seem to do the job. Adding __attribute__((deprecated)) as drafted below causes a syntax error.

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricRectangle,
   GeometricSquare = GeometricRectangle __attribute__((deprecated)),

Parse Issue Expected }

   GeometricCircle
}GeometricFigures;

So obviously that either does not work altogether or I am simply using it wrong.

like image 426
Till Avatar asked Apr 15 '12 20:04

Till


People also ask

How do I create an enum in Objective C?

Objective-C Language Enums Defining an enumtypedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB = 5, MyEnumValueC = 10, }; You can also specify on the first value and all the following will use it with increment: typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB, MyEnumValueC, };

Can enum values be changed in C?

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

Can you change the value of an enum?

Enum constants are implicitly static and final and you can not change their value once created.

Is enum 32 bit?

On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less. The GCC C compiler will allocate enough memory for an enum to hold any of the values that you have declared. So, if your code only uses values below 256, your enum should be 8 bits wide.


1 Answers

I think you're attaching the __attribute__ bit to the wrong side of the assignment operator. This seems to work fine:

typedef enum
{
   GeometricPoint,
   GeometricLine,
   GeometricRectangle,
   GeometricSquare __attribute__((deprecated)) = GeometricRectangle,
   GeometricCircle
}GeometricFigures;

and now assigning GeometricSquare gives a compiler warning:

int fig = GeometricSquare;    //'GeometricSquare' is deprecated
like image 71
jscs Avatar answered Sep 21 '22 11:09

jscs