在 Objective-C 中,如何制作一个所有人都可以访问的全局配置?

发布于 2025-01-04 10:20:51 字数 1090 浏览 1 评论 0原文

我是 Objective-C 和 iOS 开发的新手,我不确定我做得是否正确。无论如何,基本上我有一个文件 Configs.plist,目前它有两组 Keys:Value(Customer:Generic 和 Short_Code:Default)。我希望所有类都能轻松访问这些数据,因此我创建了这些:

Configs.h

 extern NSString * const CUSTOMER;
 extern NSString * const SHORT_CODE;

 @interface Configs
 + (void)initialize;
 + (NSDictionary *)getConfigs;
 @end

Configs.m

 #import "Configs.h"

 NSString * const CUSTOMER = @"Customer";
 NSString * const SHORT_CODE = @"Short_Code";

 static NSDictionary *myConfigs;

 @implementation Configs
 + (void)initialize{
     if(myConfigs == nil){
         NSString *path = [[NSBundle mainBundle] pathForResource:@"Configs" ofType:@"plist"];
         settings = [[NSDictionary alloc] initWithContentsOfFile:path];
     }
 }

 + (NSDictionary *)getConfigs{
     return settings;
 }
 @end

并在测试文件 Test.m 上:

 NSLog(@"Customer: %@", [[Configs getConfigs] objectForKey:CUSTOMER]);
 NSLog(@"Short Code: %@", [[Configs getConfigs] objectForKey:SHORT_CODE]);

问题是,这种方法有效,但我想知道是否有更好的方法做这个。

I am new to Objective-C and iOS development and I'm not sure if I'm doing it right. Anyway, basically I have a file Configs.plist which, for now has two sets of Keys:Value (Customer:Generic and Short_Code:Default). I want these data to be easily accessible to all classes so I created these:

Configs.h

 extern NSString * const CUSTOMER;
 extern NSString * const SHORT_CODE;

 @interface Configs
 + (void)initialize;
 + (NSDictionary *)getConfigs;
 @end

Configs.m

 #import "Configs.h"

 NSString * const CUSTOMER = @"Customer";
 NSString * const SHORT_CODE = @"Short_Code";

 static NSDictionary *myConfigs;

 @implementation Configs
 + (void)initialize{
     if(myConfigs == nil){
         NSString *path = [[NSBundle mainBundle] pathForResource:@"Configs" ofType:@"plist"];
         settings = [[NSDictionary alloc] initWithContentsOfFile:path];
     }
 }

 + (NSDictionary *)getConfigs{
     return settings;
 }
 @end

And on a the test file Test.m:

 NSLog(@"Customer: %@", [[Configs getConfigs] objectForKey:CUSTOMER]);
 NSLog(@"Short Code: %@", [[Configs getConfigs] objectForKey:SHORT_CODE]);

The thing is, this approach works but I want to know if there are better ways to do this.

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

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

发布评论

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

评论(1

妄想挽回 2025-01-11 10:20:52

我认为只要你的配置在执行过程中不改变就很好。如果是这样,您最好使用单例将您的配置公开为属性,这样您就可以执行以下操作:

[[Config shared] customer];
[[Config shared] setShortCode:@"CODE"];

您仍然可以从 plist 初始化配置,或者实现编码协议以将其存储在 NSUserDefaults

I think this is good as long as your configuration does not change during execution. If it does, you're better off with the singleton exposing your config as properties, so you would be able to do something like this:

[[Config shared] customer];
[[Config shared] setShortCode:@"CODE"];

You could still init the config from the plist, or implement coding protocol to store it in the NSUserDefaults.

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