I'm trying to save data in NSUserDefaults. The very first time this code (and app) loops, it should go through the if-statement. Because no data was saved in NSUserdefaults was saved yet with the names: BoughtItem0, BoughtItem1, BoughtItem2, BoughtItem3.
But somehow after looping the code for the first time, and so starting my app for the second time it keeps going through the if-statement. What's wrong in my code?
for (int i = 0; i < [shopPrices count]; i++)
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BoughtItem%d"] == nil)
{
[[NSUserDefaults standardUserDefaults] setValue:@"NONO" forKey:[NSString stringWithFormat:@"BoughtItem%d", i]];
NSLog(@"BoughtItem%d", i);
}
else
{
NSLog(@"No new bought item keys made");
}
}
the output is:
BoughtItem0
BoughtItem1
BoughtItem2
BoughtItem3