如何存储相对静态但可配置的信息 [英] How to store relatively static but configurable information

查看:164
本文介绍了如何存储相对静态但可配置的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表(字典的字典)来存储整数到字符串的映射。我有类型和子类型。例如类型1 =>水果和子类型1 =>苹果。

I have a table (dictionary of dictionaries) to store mapping of integers to strings. I have types and sub-types. e.g. type 1=>fruits and sub-type 1=>apple.

映射应该可以在我的设置界面中配置,但对于应用程序的重置,它是静态的。因此,我不想每次查看映射时都从属性列表中读取内容。我只想在程序启动时读一次,而仅在用户更改设置时才写。

The mapping shall be configurable in my setting interface, but for the reset of the application, it's rather static. So I don't want to read from property-list every time I check the mapping. I'd like to only read once when program starts and only write when user changes setting.

该表在运行时应该放在哪里?我想到了两种方法:
(1)是将整个表作为变量放置在应用程序委托中,在这种情况下,每次需要获取字符串时,我都需要两次使用objectForKey。
(2)是用静态变量创建一个类(尚不确定如何做)并使用类方法来获取映射,例如+(NSString)stringForType:子类型:

Where should I keep this table at runtime? I have two approaches in mind: (1) is to put the entire table as a variable in the application delegate, in this case, I need to use objectForKey twice every time I need to get a string. (2) is to create a class with a static variable (not sure how to do it yet) and use class method to get the mapping e.g. +(NSString) stringForType: subType:

您能否评估这两种方法或提出更好的解决方案?非常感谢!

Could you please evaluate these two approaches or suggest better solutions? Thanks a lot!

推荐答案

放在哪里并不重要,只要保留一个副本即可。 ,您应该检查的重要内容是 valueForKeyPath:。如果存储在plist中,并定期保存到plist中,那么持久性将带来很大的好处。您可以使用 [NSUserDefaults standardUserDefaults]; 对象自动获取。

it doesn't really matter where you put it, as long as you keep exactly one copy of it, the important thing that you should check out is valueForKeyPath:. If you store in a plist, and save to the plist regularly you have the great benefit of persistence. You can get that automatically with the [NSUserDefaults standardUserDefaults]; object.

这篇关于如何存储相对静态但可配置的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆