Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top