2

I'm developing an iOS application and I am planning to have a system by which the application will store a 'user profile'. This would be information about the user such as name, gender and age, the rest of the app will have to use and respond to this data. i.e. using female pronouns if the gender is female.

I considered using the NSUserDefaults class, but I would like the application to be able store multiple profiles, one of which will then be selected on startup and reflect across the application.

What's the best way to do this?

Thilo
  • 8,827
  • 2
  • 35
  • 56
Alex G
  • 23
  • 3

1 Answers1

0

If NSUserDefaults suits your needs generally, then you can use a simple trick to separate data for separate users.

Simply use a key that has the username or other identifier at the beginning of the key, and use that key to store data for that user.

A simple helper method like this will give you a key to use:

- (NSString *)generateKey:(NSString *)key forUsername:(NSString *)username {
    return [NSString stringWithFormat:@"%@:%@", username, key];
}

You could get more complicated and store all your data in an NSDictionary with username(or id) as the key and that user's settings as the value, but you may not need or desire that complexity right now.

Kekoa
  • 27,892
  • 14
  • 72
  • 91
  • Thanks for this informative & speedy response! Given that I'll be using an SQLite database for other data in the app, would it be better design to include the user system in this? – Alex G Feb 09 '13 at 07:18
  • 1
    If you are going to have data in the database for each user, it would make sense to have their settings in the database as well. If you don't have a large amount of data for each user, then NSUserDefaults is a very simple way to store data, but if you're already going through the trouble of including a SQLite database, you might want the settings there. It's really up to you. – Kekoa Feb 09 '13 at 16:11