@interface Settings : NSObject
+ (instancetype)shared;
- (id)objectForKeyedSubscript:
(NSString *)name;
- (void)setObject:(id)value
forKeyedSubscript:(NSString *)name;
@end
@implementation Settings {
NSMutableDictionary *_values;
}
//subscripts are instance-only: the
//static version goes through a
//shared instance
+ (instancetype)shared {
static Settings *instance;
static dispatch_once_t once;
dispatch_once(&once, ^{
instance = [Settings new];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_values =
[NSMutableDictionary dictionary];
}
return self;
}
- (id)objectForKeyedSubscript:
(NSString *)name {
return _values[name];
}
- (void)setObject:(id)value
forKeyedSubscript:(NSString *)name {
_values[name] = value;
}
@end
Settings.shared[@"host"] = @"192.168.100.1";
Settings.shared[@"port"] = @3306;
NSString *host = Settings.shared[@"host"];
//host is "192.168.100.1"