NSUserDefaultsを出来るだけ短く書きたいと考えた結果、こんなものを作ってみました。
ソースはこちらにあります。
@"hoge".defaultsBool = YES;
NSLog(@"%@", @"hoge".defaultsBool ? @"YES" : @"NO");
@"fuga".defaultsFloat = 3.14;
NSLog(@"%f", @"fuga".defaultsFloat);
@"url".defaultsURL = [NSURL URLWithString:@"http://qiita.com/"];
NSLog(@"%@", @"url".defaultsURL);
@"string".defaultsString = @"string";
NSLog(@"%@", @"string".defaultsString);
@"array".defaultsArray = @[@1, @2, @3];
NSLog(@"%@", @"array".defaultsArray);
@"dict".defaultsDictionary = @{@"key": @"obj"};
NSLog(@"%@", @"dict".defaultsDictionary);
間接的な利用を推奨します。
1.定義文字列経由
#define DEFAULTS_FLOAT_VAR @"defaults.var"
DEFAULTS_FLOAT_VAR.defaultsFloat = 0.01;
// @"defaults.var".defaultsFloat = 0.01; // Do not use
2.ラッパークラス経由
@interface Wrapper : NSObject
@property BOOL hoge;
@end
@implementation Wrapper
- (BOOL)hoge
{
return @"hoge".defaultsBool;
}
- (void)setHoge:(BOOL)hoge
{
@"hoge".defaultsBool = hoge;
}
@end
Wrapper *wrapper = //;
wrapper.hoge = YES;