1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NSString+NSUserDefaults

Posted at

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;
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?