views:

687

answers:

1

In the Application Delegate didFinishLaunching method, I am using the following code to build up a new NSDictionary to be used as the new settings bundle for the user:

NSNumber *testValue = (NSNumber*)[[NSUserDefaults standardUserDefaults] objectForKey:@"settingsversion"];
if (testValue == nil)
{
    NSNumber *numNewDB = [NSNumber numberWithBool:NO];
    NSNumber *numFirstUse = [NSNumber numberWithBool:YES];
    NSDate *dateLastStatic = [NSDate date];
    NSDate *dateLastMobile = [NSDate date];
    NSNumber *numSettingsversion = [NSNumber numberWithFloat:1.0];

    NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                 numNewDB, @"newdb",
                                 numFirstUse, @"firstuse",
                                 numSettingsversion, @"settingsversion",
                                 dateLastStatic, @"laststaticupdate",
                                 dateLastMobile, @"lastmobileupdate",
                                 nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

Later in another ViewController I am trying to read back a value from that same Dictionary, saved as the NSUserDefaults - well at least I thought it would, but I don't get any valid object pointer for the desired member lastUpdate back there:

in .h file:

NSDate *lastUpdate;

in the .m file in a member function:

lastUpdate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"laststaticupdate"];

Even, if I print out the content of [NSUserDefaults standardUserDefaults] I only get this:

2010-04-29 15:13:22.322 myApp[4136:207] Content of UserDefaults: <NSUserDefaults: 0x11d340>

This leads me to the conclusion that there is no standardUserDefaults dictionary somewhere in memory or it cannot be determined as such a structure.

Edit: Every time, I restart the app ión the device, the check for testValue is Nil and I am building up the dictionary again but after one run it should be persistent on the Phone, right?

Am I doing something wrong somewhere in between? I have the feeling that I yet didn't really understand how to load and save settings persistent for a certain application on the iPhone.

Is there anything I have to do additionally to this? Integrating a settings.bundle in XCode or saving the dictionary manually to the Documents folder?

Can someone help me out here? Thanks a lot!

+1  A: 

registerDefaults doesn't actually write anything to disk. It just creates a 'defaults defaults' dictionary in memory. So everytime you restart the app, testValue should be nil.

If you want to set persistent values, use setObject:forKey:.

I don't know why you aren't getting a valid object back however.

Rengers
Thanks a lot! That was the problem with saving the values. I misunderstood the documentation. :)
Nonlinearsound
Could it be that [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; references and retains the dictionary and by that gives me a valid pointer? As I didn't use setObject: I didn't get back valid entries but a valid dictionary pointer - not saved to the user defaults of course.
Nonlinearsound