maxedison
maxedison

Reputation: 17553

Best way to create an NSDictionary that will always have the same data and will be needed at different times

My app needs to convert between the singular and plural versions of certain words at various moments. I've therefore got a dictionary where the keys are the singular versions and the values are the plural versions.

I'm wondering what the best way is to reinitialize this dictionary when necessary. As I see it, I have two options:

  1. Store this data in a plist and reinitialize the dictionary from the plist
  2. Create a wrapper object for NSDictionary that simply has a single NSDictionary property, whose value is set in the init method.

My guess is that #2 is faster, but doesn't feel very elegant. Ultimately, subclassing NSDictionary seems like the right approach, but once I found out that NSDictionary is actually a class cluster, well, I chose not to do that.

Upvotes: 0

Views: 137

Answers (2)

user523234
user523234

Reputation: 14834

You can serialize/store it to disk with NSKeyedArchiver and read it back with NSKeyedUnarchiver.

Upvotes: 0

TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

Is the dictionary going to be very large? If so, why not just initialize it as an instance variable when your app activates and don't release it. Also, I'm confused by your 2nd option and your comment. You mentioned in your option that you were going to create a wrapper object for a NSDictionary, but then you said you were going to subclass it. I'm not sure if you just got the confused or what, but your 2nd option (By creating a wrapper object with 1 NSDictionary and initializing the values in the init) also seems feasible to me too. Ultimately, any option should be fine as long as it doesn't impact the performance of your app too much.

Upvotes: 1

Related Questions