Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help! Cocoa Compliation Error!

Help! Cocoa Compliation Error!
Thread Tools
Forum Regular
Join Date: Sep 2003
Location: San Diego
Status: Offline
Reply With Quote
Apr 25, 2005, 11:43 PM
 
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
Reply With Quote
Apr 26, 2005, 06:02 AM
 
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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:27 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2