2

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.

5
  • 1
    Aren't you suppose to add a semicolon at the end? And why are you defining it with typedef, while not adding an alias to the enum at the end? Commented Sep 23, 2012 at 8:08
  • 1
    And to address your problem, if the Card class is the only class that uses Suit, then you can just add Suit in your Card's header. There's no strict rule to follow where you're suppose to put an enum, just put it where classes that requires it can easily access it. Commented Sep 23, 2012 at 8:09
  • 1
    "for the Suit header class, what class type should it be?(subclass of what, NSObject or..?" <- enums are not classes. Commented Sep 23, 2012 at 8:23
  • @theAmateurProgrammer as I said I don't know much about objective-c enum. Card is gonna be used for several times in several classes. Commented Sep 23, 2012 at 10:24
  • @H2CO3 thanks for telling, Quuxplusone 's sample is much clearer. Commented Sep 23, 2012 at 10:26

1 Answer 1

7
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
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use the NS_ENUM macro to more cleanly type your enums. Look in the UIKit headers, e.g. UIView.h - they use it for all the UIView enums.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.