测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2017 年 9 月 |
由 ghost 和 James Whitfield 维护。
NEIApplicationConfig 允许开发者针对每个环境和应用程序身份定义配置,这使得部署和测试多个版本变得更容易。尽管通过 .plist 使用应用程序属性很有用,但 NEIApplicationConfig 允许您针对模拟器、设备和生产环境分别指定默认配置,同时还可以覆盖特定于应用程序身份的特定配置,例如生产、Beta、Adhoc 等。使用 NEIApplicationConfig,您将指定一个默认配置作为基线,并指定与其相关环境相关的覆盖。
NEIApplicationConfig 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod "NEIApplicationConfig"
为每个环境创建 ApplicationConfig.plist
ApplicationConfig.plist
ApplicationConfig.debug.plist
ApplicationConfig.simulator.plist
加载时,应用程序始终加载 ApplicationConfig.plist,如果启用了 DEBUG 宏,则可选性地加载 ApplicaitonConfig.debug.plist,如果应用程序在模拟器中运行,则可选性地加载 ApplicationConfig.simulator.plist。
使用这种方法,发布的应用程序将仅使用 ApplicationConfig.plist,因为它既未进行调试,也未在模拟器中运行。如果在设备上调试,则将加载并合并 ApplicationConfig.plist 和 ApplicationConfig.debug.plist,如果正在模拟器中执行,则将使用 ApplicationConfig.simulator.plist。
除了上述加载顺序外,每个配置还可以包含一个以应用程序身份识别符作为键值,其值必须是用于覆盖的字典,该字典将随后添加到合并应用程序配置的根中。这允许开发者针对特定的包配置,如 beta 和 Adhoc 等,可能需要指向不同的后端服务器,如测试。
例如,您想要测试即将发布的新增强功能,应将它们指向测试服务器而不是生产服务器。因此,您可以添加一个具有 com.myapp.beta.AppName 键的字典,其中包含指向相应 web 服务 URL 等的键。
NEIApplicationConfig 可始终使用静态单例方法 defaults() 获取默认/合并配置,或者将默认配置分配给 config 变量,以用作配置引用。
Swift
import NEIApplicationConfig
class YourViewController : UIViewController {
let config = NEIApplicationConfig.defaults()
func methodUsingConfig() {
let booleanValue = config.get(booleanForKey: "VALUE_ENABLED", defaultValue: false)
let stringWithDefaultValue = config.get(stringForKey: "STRING_KEY", defaultValue: "DEFAULT_VALUE")
let stringOrNil = config.get(stringForKey: "STRING_KEY")
config.set(object:true,forKey:"IS_AWESOME_API") //sets or overrides a value set in the config. However, setting is not persisted
if let truth = config.get(booleanForKey: "IS_AWESOME_API", defaultValue: true) {
//
}
}
}
Objective C
@implementation YouViewController {
- (void) methodUsingConfig() {
NEIApplicationConfig * config = [NEIApplicationConfig defaults];
BOOL enabled = [config booleanForKey:@"VALUE_ENABLED" value:false];
NSString * stringValue = [config stringForKey:@"STRING_KEY" value:@"defualt value"];
[config setObject:@(true) forKey:@"IS_AWESOME_API"];
if([config booleanForKey:@"IS_AWESOME_API" value:NO]) {
}
// ....
}
}
@interface NEIApplicationConfig : NSObject
+ (nonnull NEIApplicationConfig *)defaults;
+ (nonnull NEIApplicationConfig *)setObject:(nullable id)object forKey:(nonnull NSString *)key NS_SWIFT_NAME(set(value:key:));
+ (nullable NSObject *)objectForKey:(nonnull NSString *)key NS_SWIFT_NAME( get(objectForKey:) );
+ (nonnull NSArray *)allKeys NS_SWIFT_NAME(keys());
+ (nonnull NSObject *)objectForKey:(nonnull NSString *)key value:(nonnull NSObject *)value NS_SWIFT_NAME( get(objectForKey:defaultValue:) );
+ (nonnull NSString *)stringForKey:(nonnull NSString *)key value:(nonnull NSString *)default_value NS_SWIFT_NAME( get(stringForKey:defaultValue:));
+ (nullable NSString *)stringForKey:(nonnull NSString *)key NS_SWIFT_NAME( get(stringForKey:) );
+ (nullable Class)classForKey:(nonnull NSString *)key value:(nullable NSString *)value NS_SWIFT_NAME(get(classForKey:defaultValue:));
+ (nullable Class)classForKey:(nonnull NSString *)key NS_SWIFT_NAME(get(classForKey:));
+ (BOOL)booleanForKey:(nonnull NSString *)key value:(BOOL)default_value NS_SWIFT_NAME(get(boolforKey:defaultValue:));
+ (NSInteger)intForKey:(nonnull NSString *)key value:(NSInteger)default_value NS_SWIFT_NAME(get(intForKey:defaultValue:));
+ (float)floatForKey:(nonnull NSString *)key value:(float)default_value NS_SWIFT_NAME(get(floatForKey:defaultValue:));
+ (nullable CTFontRef)createFontWithName:(nonnull NSString *)name ;
+ (nullable UIFont *)fontWithName: (nonnull NSString *)name NS_SWIFT_NAME(font(named:));
+ (nonnull NEIApplicationConfig *)configWithScope:(nullable NSString *)scope NS_SWIFT_NAME(with(scope:));
+ (nonnull NEIApplicationConfig *)configWithDictionary:(nullable NSDictionary *)dictionary NS_SWIFT_NAME(with(dictionary:));
#pragma mark - Instance Methods
- (nonnull NEIApplicationConfig *)setObject:(nullable id)value forKey:(nonnull NSString *)key NS_SWIFT_NAME(set(object:forKey:));
- (nullable NSObject *)objectForKey:(nonnull NSString *)key NS_SWIFT_NAME(get(objectForKey:));
- (nonnull NSString *)stringForKey:(nonnull NSString *)key value:(nonnull NSString *)default_value NS_SWIFT_NAME(get(stringForKey:defaultValue:));
- (nullable NSString *)stringForKey:(nonnull NSString *)key NS_SWIFT_NAME(get(stringForKey:));
- (float)floatForKey:(nonnull NSString *)key value:(float)default_value NS_SWIFT_NAME(get(floatForKey:value:));
- (nullable UIFont *)fontWithName:(nonnull NSString *)name NS_SWIFT_NAME(font(named:));
- (nullable CTFontRef)createFontWithName:(nonnull NSString *)name ;
- (nonnull NSArray *)allKeys NS_SWIFT_NAME(keys());
- (nullable Class)classForKey:(nonnull NSString *)key value:(nonnull NSString *)value NS_SWIFT_NAME(get(classForKey:defaultValue:));
- (BOOL)booleanForKey:(nonnull NSString *)key value:(BOOL)default_value NS_SWIFT_NAME(get(booleanForKey:defaultValue:));
- (NSInteger)intForKey:(nonnull NSString *)key value:(NSInteger)default_value NS_SWIFT_NAME(get(intForKey:defaultValue:));
- (nonnull NSObject *)objectForKey:(nonnull NSString *)key value:(nonnull NSObject *)default_value;
#pragma mark -
- (nullable id)objectForKeyedSubscript:(nonnull NSString*)key;
- (void)setObject:(nullable id)obj forKeyedSubscript:(nonnull NSString *)key;
@end
GHOST, ghost [at] neilab.com
NEIApplicationConfig 在 MIT 证书下提供。有关更多信息,请参阅 LICENSE 文件。