 |
 |
Help! Cocoa Compliation Error!
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2003
Location: San Diego
Status:
Offline
|
|
I'm currently trying to teach myself Cocoa, and with that, Obj-C. I'm trying to write a blackjack application. I've created a file called "constants.h", and when I try to compile the application, I get the following error:
error: initializer element is not constant
Code:
#import <Cocoa/Cocoa.h>
typedef enum
{
Spades,
Hearts,
Clubs,
Diamonds
} Suit;
const float STARTING_ACCOUNT_BALANCE = 1000;
const float DEFAULT_BET_AMOUNT = 10;
NSArray* CardValues = [NSArray arrayWithObjects:@"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"J", @"Q", @"K", @"A", nil ];
The Error occurs at the NSArray* line, but it only occurs when the model and the controller are connected together.
I've googled around a bit for an answer, but I can't figure out what's going on. Any Suggestions?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
Your "CardValues" is a global variable that is initialized when your app launches to point to a dynamically-allocated array. Since the array is created dynamically (code must execute in order to create it), the address at which the resulting array is created is not constant. So you can't initialize a global variable with it. Because globals must be initialized to a constant.
What you can do is initialize CardValues to nil. Then, somewhere in your code, check the value of CardValues. If it's nil, create the NSArray and assign it to CardValues. A nicer technique would be to use either a function or an objective-C method called CardValues that returns the array and that has a static variable inside. Like this:
Code:
NSArray *CardValues()
{
static NSArray* sCardValues = nil;
if ( ! sCardValues )
{
sCardValues = [[NSArray arrayWithObjects:@"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"J", @"Q", @"K", @"A", nil ] retain];
NSCParameterAssert( sCardValues != nil );
}
return sCardValues;
}
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|