Ivan Li
Ivan Li

Reputation: 1950

Create and use an Enum in Objective-C

I'm currently developing a poker game for iOS in Objective-C, but I'm having issues with using . 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".

Where do I define my 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?

I'm not very familiar with Objective-C, so any help is appreciated.

Upvotes: 2

Views: 8333

Answers (1)

Quuxplusone
Quuxplusone

Reputation: 26949

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

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

Upvotes: 7

Related Questions