views:

111

answers:

3

I have NSUserDefaults storing a number of string variables for things like name, date of birth, address, etc. What I would like to know is how to write a code that will create a new object for each new user. For example, I have a spinning wheel that shows up immediately after the first time the user runs the app. What I want, is for that wheel to have one single option - "New User". Once that New User fills out a bunch of text fields that I am using NSUserDefaults to save, I want that user to be saved on that spinning wheel so that the next time they open up the app they have the option of returning to all of the variables that they previously put in, or creating a new user so they can input all new variables.

I know how to do everything except write the code to create new users automatically. Potentially, the program should allow for a limitless number of these user objects and then just use something arbitrary like their last name to input into the spinning wheel. I would assume that the code would need to be put somewhere in the following code used to save the NSUserDefaults:

NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
[userData setObject:txtName.text forKey:@"name"];

---EDIT FOR CLARIFICATION ---- I am able to put multiple strings into this 'userData' object already by simply adding more lines like the 2nd line from above. What I want to know is how to add 'user2Data', 'user3Data', 'user4Data', 'usernData'..........to make a potentially limitless amount of user objects to store these variables?

A: 

You could modify the following to meet your needs, to be put into your app delegate:

+ (void) initialize 
{
    if ([self class] == [MyAppDelegate class]) {        
        // initialize user defaults dictionary
        BOOL isFirstTimeRun = YES;
        ...

        NSMutableDictionary *resourceDict = [NSMutableDictionary dictionary];
        [resourceDict setObject:[NSNumber numberWithBool:isFirstTimeRun] forKey:kIsFirstTimeRunKey];
        ...

        [[NSUserDefaults standardUserDefaults] registerDefaults:resourceDict];
    }
}

...

- (void) applicationDidFinishLaunching:(UIApplication *)application 
{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstTimeRunKey] boolValue]) {
        // you could do first-time-run stuff here, such as initializing 
        // other data model elements...
    }
    ...
}

...

- (void) applicationWillTerminate:(UIApplication *)application 
{
    //  if we're quitting the app, it must have been run at least once
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstTimeRunKey] boolValue])
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:NO] forKey:kIsFirstTimeRunKey];
    ...
}
Alex Reynolds
A: 

You can add NSArray objects to NSUserDefaults.

NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
NSMutableArray *users = [NSMutableArray mutableArrayWithArray:[userData objectForKey:@"users"];

[users addObject:txtName.text];

[userData setObject:[NSArray arrayWithArray:users] forKey:@"name"];

[userData setObject:txtName.text forKey:@"name"];

If you need more than a single string for user data, you can look at using an NSDictionary object.

All that said, if you're going to be storing a lot of data, you should look at maybe saving .plist files in your app's Library directory.

Frank Schmitt
A: 

What you probably want to do is create a property list for each user, and save the list of users in your NSUserDefaults. Once a user has been selected, you can then load the contents of the property list for that specific user and load/store all data in that particular property list. The basics of this approach is to use an NSDictionary* wherever you need to read from the property list and an NSMutableDictionary* whenever you need to both read and write to it. When you first construct the property list, you simply instantiate an NSMutableDictionary. When you want to save it or load it, you can use the NSPropertyListSerialization class in order to save or load the dictionary from/to a property list.

Michael Aaron Safyan