xcode 为什么我不能在 Objective-C 中切换枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10280253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I switch an enum in Objective-C
提问by chrs
I can't seem to figure out how I should use a switch statement with my custom typedef enum. Xcode tells this error:
我似乎无法弄清楚应该如何将 switch 语句与我的自定义 typedef 枚举一起使用。Xcode 告诉这个错误:
Statement requires expression of integer type (MyEnum *) is invalid.
声明需要整数类型的表达式 (MyEnum *) 无效。
this is my enum declared over the @interface in my header
这是我在标题中的@interface 上声明的枚举
typedef enum {
A, B, C, D, E, F, G,
Ab, Bb, Db, Eb, Gb,
CSharp, DSharp, FSharp, GSharp
} Tones;
this is my property:
@property(nonatomic) Tones *tone;
这是我的财产:
@property(nonatomic) Tones *tone;
and this is my function to get the string value of the enum
这是我获取枚举字符串值的函数
- (NSString *)stringValue {
switch (self.tone) {
case GSharp:
return @"G#";
...
}
}
回答by Lukman
An enum has literal values (basically named integers), not object pointers. Thus it should be:
枚举具有文字值(基本上命名为整数),而不是对象指针。因此它应该是:
@property(nonatomic) Tones tone;