简体   繁体   中英

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.

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.

您可以使用NSKeyedArchiver将其序列化/存储到磁盘,然后使用NSKeyedUnarchiver读回。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM