xcode 在 Objective-C 中创建和使用枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12550689/
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
Create and use an Enum in Objective-C
提问by Ivan Li
I'm currently developing a poker game for iOS in Objective-C, but I'm having issues with using enums. I would like to create an enum
called "Suit" and use it in my Card class, since every card would have its deck and suit such as "Ace Spade".
我目前正在用 Objective-C 为 iOS 开发一个扑克游戏,但是我在使用enums 时遇到了问题。我想创建一个enum
名为“花色”并在我的卡片类中使用它,因为每张卡片都有自己的套牌和花色,例如“Ace Spade”。
Where do I define my enum
?
我在哪里定义我的enum
?
typedef enum Suit {
Diamond,
Club,
Heart,
Spade
}
Also, what type of class should I define the Suit enum
in (ex. NSObject
, UIView
, etc.)?
And, after I import Suit.h
in Card.h
, can I define it as property directly in Card.h
?
此外,什么类型的类的,我应该定义套装enum
中(例如。NSObject
,UIView
等)?而且,在我导入Suit.h
后Card.h
,我可以直接将其定义为属性Card.h
吗?
I'm not very familiar with Objective-C, so any help is appreciated.
我对 Objective-C 不是很熟悉,所以任何帮助表示赞赏。
回答by Quuxplusone
typedef enum {
Clubs, Diamonds, Hearts, Spades
} Suit;
typedef int Value;
struct Card {
Suit suit;
Value value;
};
or, if you really think Card
needs to be a heavyweight class, then
或者,如果你真的认为Card
需要成为一个重量级的班级,那么
@interface Card : NSObject
@property Suit suit;
@property Value value;
@end