1
我是Objective-C和iOS开发人员的新手,我不确定自己做得对不对。无论如何,基本上我有一个Configs.plist文件,它现在有两套Keys:Value(Customer:Generic和Short_Code:Default)。我想,所以我创建了这些,这些数据是很容易接触到的所有类:在Objective-C中,如何创建一个全局可访问的全局配置?
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]);
事情是,这种方法的工作原理,但我想知道是否有更好的方法来做到这一点。
谢谢你。我不太明白你的意思,但是,这是我需要阅读更多内容的一个主题。无论如何,是的,在执行期间,配置将不会被修改。配置基本上是预先建立的要求。 – yoninja 2012-02-10 17:30:14
是的,我很确定你的配置是可变的,所以我想给你一些关于如何处理可变配置的见解。但如果它是静态的,比我认为你的解决方案非常好。 – lawicko 2012-02-10 17:40:50