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 protected])
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]