Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch with typedef enum type from string

I used the typedef enum below but the switch always return the default case why ?

typedef enum {
    first,
    LatestNews,
    Opinion,
    Special,
    Sports,
    Thisweek,
} NAChannelTitle;

-(NSString *)getImageName:(NSString *)channelName {
    NAChannelTitle temp = (NAChannelTitle)[channelName stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"Temp : %@",temp);
    switch (temp) {
        case first:
            return @"background-channel-sporttitle-portrait.png";
            break;
        case LatestNews:
            return @"background-channel-title-portrait.png";
            break;
        case Opinion:
            return @"background-channel-title-portrait.png";
            break;
        case Special:
            return @"background-channel-sporttitle-portrait.png";
            break;
        case Sports:
            return @"background-channel-sporttitle-portrait.png";
            break;
        case Thisweek:
        default:
            return @"background-channel-title-portrait.png";
            break;
    }
    return nil;
}
like image 943
Bobj-C Avatar asked Dec 04 '25 02:12

Bobj-C


2 Answers

You can't convert a string to enum, since the enums names are not saved, instead, you can create a function that does it, by comparing strings, this is longer, but I don't think you have other option. a macro may help:

NAChannelTitle getEnumTitle(NSString *sTitle) {
#define CHECK_ENUM(X)   if([sTitle isEqualToString:@#X]) return X        
    CHECK_ENUM(first);
    // the same for all enum
    return defaultEnum; // add this to the enum
#undef CHECK_ENUM
}

then you may do:

NAChannelTitle temp = getEnumTitle(channelName);
NSLog(@"Temp : %d",temp);
switch (temp) {
    case first:
        return @"background-channel-sporttitle-portrait.png";
        break;
    case LatestNews:
        return @"background-channel-title-portrait.png";
        break;
    case Opinion:
        return @"background-channel-title-portrait.png";
        break;
    case Special:
        return @"background-channel-sporttitle-portrait.png";
        break;
    case Sports:
        return @"background-channel-sporttitle-portrait.png";
        break;
    case Thisweek:
    default:
        return @"background-channel-title-portrait.png";
        break;
}
return nil;
like image 80
MByD Avatar answered Dec 05 '25 19:12

MByD


This is what ppl looking for . Here is the shortest answer without any string comparison:

// Zoey.h
typedef enum {
    turnLeft,
    turnRight,
    turnTop,
    turnBottom
} arrowType;

// Zoey.m
NSString * const arrowTypeTypeArray[] = {
    @"turnLeft",
    @"turnRight",
    @"turnTop",
    @"turnBottom"
};

// A method to convert an enum to string.is it short enuff eh ?
-(NSString*) arrowTypeEnumToString:(arrowType)enumVal
{
  return arrowTypeArray[enumVal];
}

// An extra method to retrieve the int value from the C array of NSStrings
-(arrowType) arrowTypeStringToEnum:(NSString*)strVal
{
  int retVal;
  for(int i=0; i < sizeof(arrowTypeArray)-1; i++)
  {
    if([(NSString*)arrowTypeArray[i] isEqual:strVal])
    {
      retVal = i;
      break;
    }
  }
  return (arrowType)retVal;
}
like image 22
Zen Of Kursat Avatar answered Dec 05 '25 18:12

Zen Of Kursat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!