从字符串中使用类型定义枚举类型进行切换

时间:2021-11-20 16:38:23

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

我使用了下面的typedef enum,但是开关总是返回默认情况,为什么?

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;
}

4 个解决方案

#1


3  

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:

你不能将一个字符串转换成enum,因为enum名称并没有保存,相反,你可以创建一个函数来实现它,通过比较字符串,这将会更长,但是我认为你没有其他的选择。一个宏可以帮助:

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;

#2


2  

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

这就是ppl要找的。这里是最简短的回答,没有任何字符串比较:

// 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;
}

#3


1  

You're converting a string to an enum, this doesn't work. Strings are pointers, enums are numbers, and even though you can cast them, a pointer won't have the same value. You'll need something like:

将字符串转换为enum,这行不通。字符串是指针,枚举是数字,即使你可以对它们进行强制转换,一个指针也不会有相同的值。你需要类似:

if([temp isEqualToString:@"LatestNews"]){ ... }

and so on. Or some method to convert to an enum, which you can then switch on. Also those break statements are useless

等等。或者是一些转换到enum的方法,然后你可以打开它。同样,这些break语句是没有用的

#4


0  

The enum, stands for enumeration - you are just creating names for some integers.

enum表示枚举——您只是为某些整数创建名称。

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

means that first is 0, LatestNews is 1, and so on.

表示第一个是0,最新消息是1,以此类推。

In your function you are casting a NSString to NSInteger - hence you are not getting correct values.

在您的函数中,您正在向NSInteger转换一个NSString—因此您没有得到正确的值。

You need to use the [string isEqualToString:] method to compare your string to some conditional values.

您需要使用[string isEqualToString:]方法将您的字符串与一些条件值进行比较。

Enumeration declaration explained

枚举声明解释说

#1


3  

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:

你不能将一个字符串转换成enum,因为enum名称并没有保存,相反,你可以创建一个函数来实现它,通过比较字符串,这将会更长,但是我认为你没有其他的选择。一个宏可以帮助:

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;

#2


2  

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

这就是ppl要找的。这里是最简短的回答,没有任何字符串比较:

// 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;
}

#3


1  

You're converting a string to an enum, this doesn't work. Strings are pointers, enums are numbers, and even though you can cast them, a pointer won't have the same value. You'll need something like:

将字符串转换为enum,这行不通。字符串是指针,枚举是数字,即使你可以对它们进行强制转换,一个指针也不会有相同的值。你需要类似:

if([temp isEqualToString:@"LatestNews"]){ ... }

and so on. Or some method to convert to an enum, which you can then switch on. Also those break statements are useless

等等。或者是一些转换到enum的方法,然后你可以打开它。同样,这些break语句是没有用的

#4


0  

The enum, stands for enumeration - you are just creating names for some integers.

enum表示枚举——您只是为某些整数创建名称。

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

means that first is 0, LatestNews is 1, and so on.

表示第一个是0,最新消息是1,以此类推。

In your function you are casting a NSString to NSInteger - hence you are not getting correct values.

在您的函数中,您正在向NSInteger转换一个NSString—因此您没有得到正确的值。

You need to use the [string isEqualToString:] method to compare your string to some conditional values.

您需要使用[string isEqualToString:]方法将您的字符串与一些条件值进行比较。

Enumeration declaration explained

枚举声明解释说