Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefaults not working right

Having some trouble with NSUserDefaults here.

Here's how I'm creating it:

NSString *theCity = @"Test City";
[[NSUserDefaults standardUserDefaults] setObject:theCity forKey:@"SavedCity"];

Here's how I'm trying to retrieve it:

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"SavedCity"])  
    {
         NSLog(@"Key exists! %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"SavedCity"]);
    }
    else {
         NSLog(@"No city saved!");
    }   

The problem I have is that even if there IS a key for "SavedCity" (I check the pref file in the Simulator directory), it always displays "No city saved". Am I doing something wrong?

Thanks!

like image 326
sudo rm -rf Avatar asked Dec 02 '22 03:12

sudo rm -rf


1 Answers

Two things you could try.

1) Try synchronizing the user defaults after settings the string. [[NSUserDefaults standardUserDefaults] synchronize]

2) Try retrieving the string using -stringForKey:

like image 135
Alexsander Akers Avatar answered Dec 20 '22 18:12

Alexsander Akers