• 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: Is there a pattern for creating an object with global scope?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Graham Cox <email@hidden>
  • Date: Sat, 13 Apr 2013 11:33:45 +1000

On 13/04/2013, at 11:30 AM, YT <email@hidden> wrote:

> Perhaps my approach is wrong. Looking for advise.
>
> So I'd like to define a Class called Preference.
>
> In main.m I'd like to create an object called myPreferences before the code line
>
> return NSApplicationMain(argc, (const char**)); is run;
>
> I assume the object myPreferences will persist the life of the program.
>
> So how can I make the ID of myPreferences known to all objects in the program?
>
> Is this even possible?
>
> OK! So I'm trying to create an object with global scope so all other objects can get and put data in it.
>
> AND OK! Its true I'm not yet a mature Objective-C programmer.  Ya know one that doesn't know what I can't do.



Tip: don't reinvent the wheel. There is already a class, NSUserDefaults, which is designed for this purpose.

The use of global variables is strongly discouraged, but you can create the equivalent using a singleton object. NSUserDefaults provides this using +[NSUserDefaults standardUserDefaults] which is accessible from every part of your app at all times.

You probably don't really want to access preferences prior to running NSApplicationMain - I can't think of a sensible use case for that. Instead, just access each preference at the point where it's actually required - don't attempt to set up a whole bunch of state before anyone really needs it, it just slows down launch - do it lazily. For app-wide state, the NSApplication delegate method -applicationDidFinishLaunching is often a good place.

NSUserDefaults also provides a mechanism (-[NSUserDefaults registerDefaults]) for setting initial values for preferences from a plist resource which is useful if any of your default preferences are something other than 0/nil/NO.


--Graham


_______________________________________________

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

References: 
 >Is there a pattern for creating an object with global scope? (From: YT <email@hidden>)

  • Prev by Date: Is there a pattern for creating an object with global scope?
  • Next by Date: Re: Is there a pattern for creating an object with global scope?
  • Previous by thread: Is there a pattern for creating an object with global scope?
  • Next by thread: Re: Is there a pattern for creating an object with global scope?
  • Index(es):
    • Date
    • Thread