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 > Another question: global variables

Another question: global variables
Thread Tools
nufferkay
Fresh-Faced Recruit
Join Date: Dec 2003
Status: Offline
Reply With Quote
Dec 17, 2003, 10:38 PM
 
Is there a way to make a single object available to all other objects in the app without using global variables?

f'rinstance, is there some way to attach it to another globally available object (such as the NSApplication sharedApplication object) without subclassing NSApplication?

Thanks again for any help you can give me!

-N
     
ambush
Banned
Join Date: Apr 2002
Location: -
Status: Offline
Reply With Quote
Dec 17, 2003, 10:51 PM
 
Originally posted by nufferkay:
Is there a way to make a single object available to all other objects in the app without using global variables?

f'rinstance, is there some way to attach it to another globally available object (such as the NSApplication sharedApplication object) without subclassing NSApplication?

Thanks again for any help you can give me!

-N
I don't know if this is what you're looking for, but there's a singleton implementation for objc (used to share objects)

like +[NSApplication sharedApplication] but for YOUR object

make sure you don't use it with objhects you instantiated in IB, tho.

Code:
int the .h // Singleton + (SomeController *)sharedController; in the .m + (SomeController *)sharedController { static id sharedInstance = nil; if ( sharedInstance == nil ) { sharedInstance = [[self alloc] init]; } return sharedInstance; }
of course replace SomeController with your classe's name.
     
nufferkay  (op)
Fresh-Faced Recruit
Join Date: Dec 2003
Status: Offline
Reply With Quote
Dec 17, 2003, 11:24 PM
 
Hi ambush -

What you described makes sense, but I'm not 100% clear on this.

Basically, say I want a MyObject that I can access from anywhere in my app. From what I understand, this is what I'd do - and please correct me if/where I'm wrong:

in MyObject.h, I declare a class method:
Code:
+ (MyObject *)sharedObject;
in MyObject.m, I implement the above method as follows:
Code:
+ (MyObject *)sharedObject { static id sharedInstance = nil; if ( sharedInstance == nil ) { sharedInstance = [[self alloc] init]; } return sharedInstance; }
And finally, from anywhere in my codebase where myObject.h has been #import-ed, I can retrieve this persistent instance into a variable by sending a message like:
Code:
theSharedInstance = [MyObject sharedObject];
- and it will retrieve the same instance no matter where or when I call it (or create it if it hasn't been called for yet).

Is that right? That seems very clever and elegant to me! Much better than a random global variable, to be sure. Is this how things like [NSUserDefaults standardUserDefaults] work?

Also, I suppose that I needn't ever retain or release this object until the application is about to exit - how do I handle releasing it at that point (or should I not worry b/c the memory will be released anyway when the app exits)?

Thank you for taking the time to explain this!

-N
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Dec 18, 2003, 02:52 AM
 
Originally posted by nufferkay:
Is that right? That seems very clever and elegant to me! Much better than a random global variable, to be sure. Is this how things like [NSUserDefaults standardUserDefaults] work?
Well, the implementation you posted isn't thread-safe, but that's the general idea.

Also, I suppose that I needn't ever retain or release this object until the application is about to exit - how do I handle releasing it at that point (or should I not worry b/c the memory will be released anyway when the app exits)?
No, you don't have to release the object. The system reclaims your application's memory when it's closed.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
 
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
Top
Privacy Policy
All times are GMT -4. The time now is 12:36 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,