ios如何用字符串实现一个开关/案例?

时间:2013-06-06 19:03:08

标签: iphone ios ipad switch-statement

我想实现一个开关/案例,我可以使用字符串来评估开关案例。

switch (tmp) {            
    case one: 
        NSLog(@"the string value of tmp is one");
        break;

你们中的任何人都知道如何实现这样的目标?

我真的很感谢你的帮助

1 个答案:

答案 0 :(得分:0)

你不能在switch case语句中使用string,你只能使用int或char数据类型。 但正如我认为你的问题是让开关盒更容易理解或可读。 所以你可以为此制作枚举,例如:

typedef enum {
    zero,//by default the value starts from zero. 
    one,
    two
} NumCount;
  

在你使用它的时候。

NumCount tmp = one;

switch (tmp) {            
    case one: 
        NSLog(@"the string value of tmp is one");
        break;
}
  

我想你明白我想说什么。如果您有任何疑问,请向我询问。