-1

I created an object for a make believe user that stores the username and password, I then put the object in a dictionary. Now I'm trying to store the dictionary using NSUserDefault but its not letting me. I understand its best not to use NSUserDefault to store user data but this is just beginner stuff I'm trying to learn.

/**interface file **/
#import "newUser.h"

@interface BWViewController : UIViewController
{

    IBOutlet UITextField *userNameField;
    IBOutlet UITextField *passwordField;
    IBOutlet UILabel * loginStatus;

}

@property (nonatomic, copy) NSDictionary * aUser;

-(IBAction)signUpButton;

@end


/**implementation file**/


@interface BWViewController ()

@end

@implementation BWViewController

@synthesize aUser;

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(IBAction) signUpButton
{

    newUser * firstUser = [[newUser alloc] init];
    firstUser.userName = userNameField.text;
    firstUser.password = passwordField.text;

    NSLog(@"%@", firstUser.userName);  /**for testing**/
    NSLog(@"%@", firstUser.password);  /**for testing**/

    aUser = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:firstUser, nil] forKeys:[NSArray arrayWithObjects:userNameField.text, nil]];

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:aUser forKey:userNameField.text];
    [defaults synchronize];

    loginStatus.text = @"Thanks for signing up!";

}
Brosef
  • 2,945
  • 6
  • 34
  • 69

1 Answers1

0

The better way to do that is store two user defaults one for username and one for password:

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:firstUser.userName forKey:@"USERNAME"];
[defaults setObject:firstUser.password forKey:@"PASSWORD"];

[defaults synchronize];

It doesn't work because you want to store nsdictionary with nsarray with custom object (firstUser) but NSUserDefaults doesn't allow you to store custom object. Hope this help

//Extended

By saveForKey: and newUseFromKey: I mean something like that:

+(void)saveForKey:(NSString*)key
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:self.userName forKey:[NSString stringWithFormat:@"%@_USERNAME", key]];
    [defaults setObject:self.password forKey:[NSString stringWithFormat:@"%@_PASSWORD", key]];
    //.... other properties

    [defaults synchronize];
}

+(newUser)newUseFromKey:(NSString*)key
{
    newUser * user = [[newUser alloc] init];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    user.userName = [defaults objectForKey:[NSString stringWithFormat:@"%@_USERNAME", key]];
    user.password = [defaults objectForKey:[NSString stringWithFormat:@"%@_PASSWORD", key]];
    //.... other properties
    return user;

}

It's good solution if you don't store too many newUser object and if they don't have too many properties. The best option is put all of your object to array/dictionary (It's up to you, there is no best option between this two) and save it to file. Just remember that if you want to save custom object to file system you have to implement NSCoding protocol.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • Thanks for the info greg. I wanted to store everything in my custom object (firstUser) - information such as username, password, location, age, etc and simply use the username as the key to access the custom object and all its containing data. Looks like encoding is the only way... – Brosef Dec 04 '13 at 08:51
  • You can add to your class (newUser) two method - saveForKey: and newUseFromKey: which will save itself to user defaults and create object from defaults. But if you are going to save lots of object don't use nsuserdefaults. You can add your object to the array and save it to file. – Greg Dec 04 '13 at 09:00
  • Not sure I understand what you mean by saveForKey: and newUseFromKey:? You think it would be best to store the object in an array and save it to file? Wouldn't it be better to save an object in a dictionary, then save the dictionary to file? – Brosef Dec 04 '13 at 23:23
  • Thanks for that explanation! If I want to add additional properties to my object, what is the proper way of storing and retrieving the user info? – Brosef Dec 06 '13 at 02:37
  • It depends how many new properties you want to add and what type are they. The other question is how many newUser object are you going to store. If the properties going to be simple data type (float, int, nsstring) and you will store not too much data you can stick with user defaults, If you are going to store a lot of newUser object with not simple data type (NSArray, Images, etc.) you should consider Core Data if it's something between maybe save it to the array and store it on file system. – Greg Dec 06 '13 at 09:15