Why doesn't my NSUserDefault value get written?
I'm trying to make use of NSUserDefaults to keep track of some promotional data in my iOS app, but it's not working like I'm expecting it to. Here's the code...
NSString *promoID = [[[self currentPromo] allKeys] obje开发者_JAVA技巧ctAtIndex:0];
if(![[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"promoid-%@", promoID]])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"promoid-%@", promoID]];
.
.
.
Everytime this if statement is executed, the code inside is executed. Shouldn't the code inside this if statement only execute once?
Thanks so much for your wisdom!
Depends on how fast you go through this again.
NSUserDefaults are not synchronized instantly. If you want to do this, you need to call [[NSUserDefaults standardUserDefaults] synchronize];
after setting a value.
Use synchronize keyword to retrieve your data in NSUserDefaults
[[NSUserDefaults standardUserDefaults] synchronize];
This will retrieve and display the data.
精彩评论