|
|
Another question: global variables
|
|
|
|
Fresh-Faced Recruit
Join Date: Dec 2003
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Banned
Join Date: Apr 2002
Location: -
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Dec 2003
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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 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
|
|
|
|
|
|
|
|
|
|
|
|