I have done this code to store score.
But this is giving me the last added score only, not storing data every time on new index.
-(IBAction)btnSaveScore:(id)sender
{
if(!dictWinData)
dictWinData = [[NSMutableDictionary alloc] init];
array = [NSMutableArray arrayWithObjects:txt_EnterName.text,
[NSString stringWithFormat:@"%i",iTap], nil];
int increment = 0;
NSLog(@"array data is:--> %@",array);
for (int intWinData = 1; intWinData < [array count]; intWinData++)
{
[dictWinData setObject:array forKey:[NSString stringWithFormat:@"NameScore%d",increment]];
increment++;
}
}
If any mistake is there in my code then please let me guide for this.
Is there anyother way to store the data..?
Is NSUserDefaults helpful to store & display data…?
How to use NSUserDefaults to store & retrive data.
Thanks.
The issue is with this line:
Every time you press the save button the
incrementwill be initialized to zero and hence every time the value is added to dictionary for same keyNameScore0. Hence it will over write the existing dictionary value.You need to make the
incrementas static likestatic int increment = 0;or make it as a global variable, it will solve the issue.NSUserDefaultsis used for displaying the application setting in the ios device’s settings pane. Don’t save your application data there.Instead of
NSUserDefaultsyou can useplistto store data.Please check the link