i am new to iPhone programming..
my app is just like a quiz.. it has different type of quizs and many question in each quiz type i want to save the values like strings(quiz开发者_运维百科 name) and integers(question number) when an application is closed so when the app is restarted i want to continue where it was stopped by using saved values
How to do this...?
Can any body help me to do this please....
Thank u
To save a string:
NSString *name = @"John";
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:name forKey:@"Name"];
To load a string:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *name = [prefs stringForKey:@"Name"];
Read more at: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
I would suggest you look at Core data and save answers as they are entered. Then when the app is restarted you need to find a way to quickly load the relevant data (Actually, you will find you don't need to load that much, which could also help you reduce what you need to save.)
Remember, simplicity is of the essence, and lazy loading very helpful. Don't load anything you don't need until you need it.
A good Apple example was the SQLiteBooks sample which they provided with iPhoneOS 2.0 (I don't know if they still provide it).
NSUserDefaults is good for saving state and preferences.
精彩评论