加载配置常量动态调用设置器?

发布于 2024-08-19 00:39:22 字数 402 浏览 5 评论 0原文

我正在寻找一种设置配置常量的好方法。我的想法是使用我的配置属性创建一个单例。说出 mySingletonConf:URL、USERID、PASSWORD。在sharedInstance初始化期间,应该读取配置文件来设置mySingletonConf中的属性。

  1. 我应该使用这种类型的常量的属性吗?我认为它们应该是类级别的属性?
  2. 是否可以动态设置配置? IE。 通过读取 mySingletonConf 的所有 Setter-Methods,然后在加载的配置(.plist-Dictionary)中搜索 key == 属性名称,然后使用该值调用 setter?

如果需要新的常量,最好动态设置这些内容。然后我只需要创建新属性并调整配置文件。 我走在正确的轨道上吗? 感谢您的帮助!

I'm looking for a nice way to set my configuration constants. My idea was to create a singleton with my config properties. Say mySingletonConf with: URL, USERID, PASSWORD. During the initialization of the sharedInstance, a configuration file should be read to set the properties in mySingletonConf.

  1. Should I use properties for this type of constants? I take they should be class-level properties?
  2. Is it possible to set the configuration dynamically? I. e.
    by reading all Setter-Methods of mySingletonConf, then searching the loaded configuration (.plist-Dictionary) for the key == property name and then invoke the settter with the value?

It would be nice to have the things set dynamically, in case new constants are needed. Then I would just have to create new properties and adjust the configuration files.
Am I on the right track?
Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[旋木] 2024-08-26 00:39:22

NSUserDefaults可以处理很多事情,它确保配置是特定于用户的,因此,如果同一台 Mac 上的多个用户使用您的程序,他们可以独立配置您的程序。 Interface Builder 中还有一个对象用于绑定用户界面元素,使事情变得更加容易。如果您确实想要在系统范围内进行配置,则应该使用 Core Foundation 首选项实用程序

要存储密码,您可以使用 Apple 的钥匙串服务。用户可以指定允许哪些程序使用存储的密码(最好是您的密码)。如果密码不是特别重要,那么将密码存储在 NSUserDefaults 中也是一种选择。

不要重新发明轮子;为您提供应用程序范围的用户或主机特定的配置服务。

NSUserDefaults can take care of a lot of stuff and it ensures that the configuration is user-specific, so if multiple users on the same Mac use your program they can configure your program independently. There is also an object in Interface Builder for binding your user interface elements to, making things even easier. If you do want to make your configuration system-wide, you should use the Core Foundation Preference Utilities.

For storing passwords, you can use Apple's Keychain Services. A user is able to specify which programs are allowed to use the stored password (which would ideally be just yours). Storing passwords in NSUserDefaults is also an option if the password is not of any particular importance.

Don't re-invent the wheel; application-wide user-or-host-specific configuration services are provided for you.

痴情换悲伤 2024-08-26 00:39:22

嗯,这是可能的:)
我最终编写了提到的 Singleton,它具有只读公共属性和类内的读写访问权限(为此使用了类别,请参阅 私有 setter 示例)。

在初始化期间,类的变量将填充 .plist 文件中的值。我使用运行时 API 来获取变量列表(只需在 Stackoverflow 上搜索“objective-c 变量列表”)并使用 var 名称作为键从 returned.plist 字典中获取值。

这些值几乎可以用作常量:

MyConstants* testConstants = [MyConstants sharedInstance];
NSLog(testConstants.PARAM1);

Well, it is possible :)
I ended up writing the mentioned Singleton, which has readonly public properties and readwrite access from within the class (used Categories for that, see private setter example).

The vars of the class are filled with values from the .plist file during the initialization. I used the Runtime API to get the list of variables (just search for "objective-c list of variables" on Stackoverflow) and get the value from the loaded.plist dictionary using the var name as key.

The the values can be use almost as constants:

MyConstants* testConstants = [MyConstants sharedInstance];
NSLog(testConstants.PARAM1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文