NSArray as a class variable
NSArray as a class variable
- Subject: NSArray as a class variable
- From: Michael Craig <email@hidden>
- Date: Fri, 25 Dec 2009 23:52:31 -0500
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 (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