Re: NSArray as a class variable
Re: NSArray as a class variable
- Subject: Re: NSArray as a class variable
- From: Graham Cox <email@hidden>
- Date: Sat, 26 Dec 2009 17:23:04 +1100
On 26/12/2009, at 3:52 PM, Michael Craig wrote:
> I'm building a class Card that represents a playing card in a simulation. I
> want instances of Card to be initialized with two integers that represent
> the suit and value. When an instance of Card is queried for its suit or
> value, it returns an NSString (@"Club", @"Spade", @"4", @"King", etc.). So
> I'd like to have class variables that are NSArrays of NSStrings for suits
> and values. I declare them in Card.h as follows (I'll just show the suits
> array, for brevity):
Another tip unrelated to your original problem, but might be of interest in general. There are only 4 suits, so using an integer could easily end up as an illegal value. You could declare an enumerated type:
typedef enum
{
kClubs = 0,
kDiamonds,
kSpades,
kHearts
}
Suit;
Then you can pass around the 'Suit' type and in the debugger it will directly show you its value as kHearts, kClubs, etc - no need to mentally translate from a number to the suit. It's also far easier to trap and detect the case where an illegal value is assigned. Enumerated types are implemented as integers internally, so can still be used as array indexes if necessary.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden