Reputation: 2364
I saw several questions on this problem, but none about iOS.
I have two enums like following :
// My first enum eColor
typedef enum
{
eColorRed = 1,
eColorGreen,
eColorBlue
} eColor;
// My second enum eShape
typedef enum
{
eShapeCircle = 1,
eShapeSquare,
eRectangle
} eShape;
I would like to have a method which accept both of these enums like this :
+ (NSString*) toStr:(bothEnum)e
{
NSString *result = nil;
if (bothEnum == eColor)
{
switch(e) {
case eColorRed:
result = @"red";
break;
case eColorGreen:
result = @"green";
break;
case eColorBlue:
result = @"blue";
break;
default:
result = @"unknown";
}
}
else if (bothEnum == eShape)
{
switch (e) {
case eShapeCircle:
result = @"circle";
break;
case eShapeSquare:
result = @"square";
break;
case eShapeRectangle:
result = @"rectangle";
break;
default:
result = @"unknown";
}
}
return result;
}
Is a thing like this possible ?
I don't want to have methods like colorToStr:
, shapeToStr:
, etc. My wish is to have only one method called toStr:
, as above...
Upvotes: 0
Views: 938
Reputation: 952
You may try this approach: You make the second enum to start indexing from the last index of the first enum, then you just use single witch in your method. Remember enum type is just an int type indeed.
// My first enum
typedef enum
{
eColorRed = 1,
eColorGreen,
eColorBlue,
eColorLastIndex
} eColor;
// My second enum
typedef enum
{
eShapeCircle = eColorLastIndex,
eShapeSquare,
eShapeRectangle,
} eShape;
typedef int eTypes;
+(NSString*)toStr:(eTypes)e
{
NSString *result = nil;
switch(e) {
case eColorRed:
result = @"red";
break;
case eColorGreen:
result = @"green";
break;
case eColorBlue:
result = @"blue";
break;
case eShapeCircle:
result = @"circle";
break;
case eShapeSquare:
result = @"square";
break;
case eShapeRectangle:
result = @"rectangle";
break;
default:
result = @"unknown";
break;
}
return result;
}
Instead eGraphics you may use just int or eColorShape, whatever.
Upvotes: 2
Reputation: 17595
Update for 64-bit Change: According to apple docs about 64-bit changes,
Enumerations Are Also Typed : In the LLVM compiler, enumerated types can define the size of the enumeration. This means that some enumerated types may also have a size that is larger than you expect. The solution, as in all the other cases, is to make no assumptions about a data type’s size. Instead, assign any enumerated values to a variable with the proper data type
So you have to create enumeration with type as below syntax if you support for 64-bit.
typedef enum eColor : NSUInteger {
eColorRed = 1,
eColorGreen,
eColorBlue
} eColor;
then go with @user2260054's answer, otherwise, it will lead with below warning in 64-bit environment.
Otherwise, it will lead to warning as Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to eColor
Upvotes: 1
Reputation: 522
Enums are just constants and at run time they are just numbers. So your method doesn't know what is eColorRed, it knows it as 1, and your only option is to pass additional parameter, telling your method wether 1 passed in first argument is eColorRed or eShapeCircle. It can be just a string, like:
+ (NSString*) toStr:(NSUInteger)e fromEnumType:(NSString*)type
{
if([type isEqualToString:@"eColor"])
{
switch(e)
...
}
else if([type isEqualToString:@"eShape"])
{
switch(e)
...
}
}
Upvotes: 3
Reputation: 2185
This is not good for design point of view. Cohesion
is broken over here. While programming OOP concept must be implemented for reuse of code and decoupling the objects as well.
Upvotes: 0