Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
I know I can get the value of a variable in the settings with this code:

Code:
NSString *str_Value = [[NSUserDefaults standardUserDefaults] stringForKey:@"settings_Value1"];

But is it also possible to set the value from within the code? And if so, how?
 
Lol, yeah :)

But what about strings? The reference only suggests setting-functions for bool, float, int and object...
 
Apparently, you can use the object-method (thanks smasher):

Code:
[[NSUserDefaults standardUserDefaults] setObject: @"hiMom"  forKey: @"settings_Value1"]];

So if I understand the NSUserDefaults-class correctly, I can create settings that wont even appear in the settings-pane that the user has access to? Or is there a "NSSystemDefaults"-class or something that takes care of that?
 
Apparently, you can use the object-method (thanks smasher):

Code:
[[NSUserDefaults standardUserDefaults] setObject: @"hiMom"  forKey: @"settings_Value1"]];

So if I understand the NSUserDefaults-class correctly, I can create settings that wont even appear in the settings-pane that the user has access to? Or is there a "NSSystemDefaults"-class or something that takes care of that?
userDefaults don't appear in the Settting app unless you've define them as such via a Settings Bundle.

P.S. An NSString is a serialized subclass of NSObject. Any serialized object can be stored in NSUserDefaults.
 
userDefaults don't appear in the Settting app unless you've define them as such via a Settings Bundle.

P.S. An NSString is a serialized subclass of NSObject. Any serialized object can be stored in NSUserDefaults.

Thats great - I was thinking I had to save the settings I didnt want to expose to the user in a separate file, but this way is much handier.

Ive only created NSUserDefaults using the Settings.bundle (in the xml-editor), so Im not sure how to instantiate a NSUserDefault-object without it.

I know the syntax for instantiating is:

Code:
// Instantiate NSUsersDefaults:
NSUserDefaults *myUserDefaults = [NSUserDefaults standardUserDefaults];

// And the rest - getting and setting - we've already covered:
// Set:
[myUserDefaults setObject:@"felix" forKey:@"Username"];
// Get:
[[NSUserDefaults standardUserDefaults] stringForKey:@"Username"];

But where do I put the instantiation-code? Do I put it in the Delegate.h/m-files like this:

Code:
// myDelegate.h
// ----------------
@interface myDelegate : NSObject <UIApplicationDelegate, NSUserDefaults>
{
  NSUserDefaults *myUserDefaults = [NSUserDefaults standardUserDefaults];
}

// myDelegate.m
// ----------------
@synthesize myUserDefaults;

But how do I go about setting the default-values for them? If I do that in the delegate-code each time the app starts, wouldnt I overwrite the settings from the last session with my initialization of the defaults? This is a bit onfusing..
 
Thats great - I was thinking I had to save the settings I didnt want to expose to the user in a separate file, but this way is much handier.

Ive only created NSUserDefaults using the Settings.bundle (in the xml-editor), so Im not sure how to instantiate a NSUserDefault-object without it.

I know the syntax for instantiating is:

Code:
// Instantiate NSUsersDefaults:
NSUserDefaults *myUserDefaults = [NSUserDefaults standardUserDefaults];

// And the rest - getting and setting - we've already covered:
// Set:
[myUserDefaults setObject:@"felix" forKey:@"Username"];
// Get:
[[NSUserDefaults standardUserDefaults] stringForKey:@"Username"];

But where do I put the instantiation-code? Do I put it in the Delegate.h/m-files like this:

Code:
// myDelegate.h
// ----------------
@interface myDelegate : NSObject <UIApplicationDelegate, NSUserDefaults>
{
  NSUserDefaults *myUserDefaults = [NSUserDefaults standardUserDefaults];
}

// myDelegate.m
// ----------------
@synthesize myUserDefaults;

But how do I go about setting the default-values for them? If I do that in the delegate-code each time the app starts, wouldnt I overwrite the settings from the last session with my initialization of the defaults? This is a bit onfusing..

[[NSUserDefaults standardUserDefaults] registerDefaults:myDefaultsDictionary];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.