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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:35:24  来源:igfitidea点击:

Create and use an Enum in Objective-C

objective-ciosxcodeenums

提问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 enumcalled "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 enumin (ex. NSObject, UIView, etc.)? And, after I import Suit.hin Card.h, can I define it as property directly in Card.h?

此外,什么类型的类的,我应该定义套装enum中(例如。NSObjectUIView等)?而且,在我导入Suit.hCard.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 Cardneeds to be a heavyweight class, then

或者,如果你真的认为Card需要成为一个重量级的班级,那么

@interface Card : NSObject
@property Suit suit;
@property Value value;
@end