views:

65

answers:

2

I have created a Settings.bundle for my application, and I try to read the settings by the following code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(!defaults)
{
    NSLog("can not get default bundle");
}
NSString *usr = [NSString stringWithFormat:@"%@",[defaults stringForKey:@"username"]];
NSLog(usr);

The output in the console is (null). I have used this before and it worked. This situation happened after I upgraded to the latest xcode for iOS. I am not sure if there is any change?

Here is the screen shot from my Settings.bundle.

alt text

A: 

stringForKey: is returning nil.

It's returning nil because it can't find a key for "username".

It can't find a key for "username" because the key doesn't exist in the user defaults for that app.

The key doesn't exist because either (A) it never existed, for that app, or (B) it existed but it was deleted.

(A) Can happen on the device or simulator if the app gets deleted via Springboard, or if the entire device (Simulator) gets reset. Are either of those two scenarios true?

(B) Can happen if you'd done something in your code to delete the key. Can you confirm that is not the case?

Shaggy Frog
I can confirm that this is not related to the two scenarios.
nababa
Are you certain that a value for "username" is getting saved and persisted by your app before application exit?
Shaggy Frog
yes, there is a default value and I can see the default value in the "Settings" in the simulator.
nababa
A: 

If you switch Simulators (new or regression SDK) or run different OS versions in Simulators, any session information, including defaults, might not carry between these different Simulator runs.

hotpaw2
However, I am still getting nil after I manually typed in the username in the settings.
nababa
Before or after you ran the app (or both preferably)?
hotpaw2