Re: Global Object
Re: Global Object
- Subject: Re: Global Object
- From: Jens Alfke <email@hidden>
- Date: Fri, 27 Nov 2009 23:10:22 -0800
On Nov 26, 2009, at 9:18 AM, Tom Jones wrote:
> I thought I could just create a Global variable but that does not work.
Did you make it a pointer? You can't directly declare instances of any Cocoa classes, only pointers to them.
So
MyCocoaClass gFoo;
is a syntax error, while
MyCocoaClass *gFoo;
works.
Of course, you need some initialization code that allocates a new object and assigns it to foo. And you have to be sure that this code will run before anyone else tries to use gFoo. This can be problematic. Putting this code into your app delegate's awakeFromNib method is often sufficient, via something like this:
- (void) awakeFromNib {
gFoo = [[MyCocoaClass alloc] init];
}
Another way is to do the initialization in a class's +initialize method.
—Jens_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden