Re: Is there a pattern for creating an object with global scope?
Re: Is there a pattern for creating an object with global scope?
- Subject: Re: Is there a pattern for creating an object with global scope?
- From: Jens Alfke <email@hidden>
- Date: Fri, 12 Apr 2013 21:08:22 -0700
On Apr 12, 2013, at 6:54 PM, Scott Ribe <email@hidden> wrote:
> Yes, extremely easy, just "create" the var, as in:
>
> int gFoobar = 42;
YT wants to create an object, which isn’t as straightforward because you can’t have an object literal in Objective-C. Instead you declare a global pointer and initialize it early on.
MyClass* gMyObject;
Then early at launch time:
gMyObject = [[MyClass alloc] init];
That second line could go into the -applicationDidFinishLaunching: method, which is the usual place where stuff gets initialized.
That said, Graham is right that you should use NSUserDefaults to store preferences instead of making your own class.
—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