• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Global Object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Global Object
      • From: Tom Jones <email@hidden>
References: 
 >Global Object (From: Tom Jones <email@hidden>)

  • Prev by Date: Re: Getting the path to an application with a launchDaemon
  • Next by Date: Re: Referencing NSData's length in a predicate?
  • Previous by thread: Re: Global Object
  • Next by thread: Re: Global Object
  • Index(es):
    • Date
    • Thread