NSArray as a class variable
Hi folks, 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): #import Cocoa/Cocoa.h static NSArray *suitArray; @interface Card : NSObject { ... They're initialized in Card.m as follows: @implementation Card +(void) initialize { suitArray = [NSArray arrayWithObjects: @Club, @Diamond, @Spade, @Heart, nil]; } ... However, when I try to access suitArray from another of Card's methods, I get an EXC_BAD_ACCESS error in the console. Here's a simplified version of the method (that I've tested and which produces that error): -(void) testIt: (id) sender { NSString *str = [NSString stringWithString: [suitArray objectAtIndex: 2]]; NSLog(@%@, str); } If I declare str as a class variable and set it inside initialize, then I get the desired output from the NSLog call in testIt:. If I declare it as a class variable and set it inside testIt:, as shown above, I still get the error. What's going on here? NSArray's elements have ref. count 1, right? Is this related? Thanks, Michael S Craig ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com
Re: NSArray as a class variable
Presumably you're not using garbage collection. If that's the case, suitArray is being deallocated because you didn't retain it. Probably time to check out the memory management guidelines: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html Particularly the bit under Obtaining Objects Using Convenience Methods ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com
Re: NSArray as a class variable
Ah, that makes perfect sense. Thank you! Michael S Craig On Sat, Dec 26, 2009 at 12:12 AM, Dave Keck davek...@gmail.com wrote: Presumably you're not using garbage collection. If that's the case, suitArray is being deallocated because you didn't retain it. Probably time to check out the memory management guidelines: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html Particularly the bit under Obtaining Objects Using Convenience Methods ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com
Re: NSArray as a class variable
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 (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com
Re: NSArray as a class variable
On Fri, Dec 25, 2009 at 10:52 PM, Michael Craig mks...@gmail.com wrote: #import Cocoa/Cocoa.h static NSArray *suitArray; I doubt you really mean this. When you put this in your header file, every file that includes Card.h has its own, separate copy of suitArray. If you want to export a shared global into every file, then Card.h should have: extern NSArray *suitArray; and Card.m: NSArray *suitArray; If you only want one copy, only visible from your implementation file, then put this in Card.m: static NSArray *suitArray; ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com