On 13/04/2013, at 11:30 AM, YT <y...@redwoodcontent.com> 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to