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 > Global variables in Cocoa

Global variables in Cocoa
Thread Tools
Dedicated MacNNer
Join Date: Jan 2001
Location: Computer Error: Unknown
Status: Offline
Reply With Quote
Apr 23, 2001, 07:07 PM
 
Where do I store a variable in Cocoa that is available to all of the code in the program?
"...Because the people who are crazy enough to think they can change the world,
are the ones who do."
-To the Crazy Ones

     
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Apr 23, 2001, 07:18 PM
 
Anywhere. It's just the same as in C. For example, you could declare an integer in main.m just above the main function like:
int myInt = 0;

It has an initial value of 0. In all of the other files that you want to have access to that integer, just put at the top of the file:

extern int myInt;

Then you can acces myInt everywhere. Just don't say:

extern myInt = 0;

Or else it'll throw an error at you. Only do the initialization at it's declaration.
     
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status: Offline
Reply With Quote
Apr 23, 2001, 11:13 PM
 
Another way to do it is to subclass NSApplication to provide some extra global data storage. I'm probably going to use this method to provide global access to an array of icons which are used in rather random areas of a program.

------------------
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Computer Error: Unknown
Status: Offline
Reply With Quote
Apr 24, 2001, 06:49 AM
 
Originally posted by mr_sonicblue:
Another way to do it is to subclass NSApplication to provide some extra global data storage. I'm probably going to use this method to provide global access to an array of icons which are used in rather random areas of a program.

How would I do this?
"...Because the people who are crazy enough to think they can change the world,
are the ones who do."
-To the Crazy Ones

     
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Apr 24, 2001, 07:42 AM
 
You should actually try to avoid this where possible. It is bad programming style to get into globals because they don't allow you to "hide" implementation of any preprocessing that you might need it for some times. Also, you run the risk of forgetting what you are doing some time in later development (so only use a few if you have to use them at all). Best way to handle all of your data is to ensure that everything is tied to some object (worst comes to worst I use a "DataBank" object that everything can access) so that any operations being performed on the variables can be abstracted (happens without you having to think about it). This is also a VERY important in large programs where you are garruanteed to forget what you are doing sometime during development.

The upside is you get highly organized code and you only have to think about one process at a time (for example, you can assume that moveCrapAroundAndFutzWithThisNumber does exactly what it claims so you don't have to perform all those operations or even bother thinking about the scope of them) but the downside is that design modifications made late in development are a bitch to implement.

Sweet, Sweet University of Waterloo,
Jeff.
Spectral Class
"Shedding Light on Innovation"
     
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status: Offline
Reply With Quote
Apr 24, 2001, 12:53 PM
 
Yea, globals are bad, in general. But they do make things tons easier in extreme cases.

Originally posted by graphiteman:
How would I do this?
Here's all I think you have to do:

1) Create a subclass of NSApplication in Interface Builder.
2) Create the files for you subclass.
3) Change the custom class of "File's Owner" to your subclass.
4) Just to check, save and exit Interface Builder. Go to the Applications Settings tab under Targets. In the "Cocoa-Specific" section, "Prinicpal class" should have changed to the name of you subclass.

------------------
     
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Oulu, Finland
Status: Offline
Reply With Quote
Apr 26, 2001, 02:58 PM
 
Also adding new method to NSApplication or NSObject by categories. Of course you can't add new attributes with categories, but it is possible to use static variable in new method. If you add it into the NSObject class, value is available in every object inherited.

More info about categories from "Object-oriented Programming and the Objective-C Language" pages 91-94. Available from http://www.apple.com/developer .

--TKT
     
   
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 01:25 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