Created
August 9, 2012 02:21
-
-
Save tochi/3300368 to your computer and use it in GitHub Desktop.
NSUserDefaultsを使うときはWrapして使うと便利 ref: http://qiita.com/items/2af840ded249b3e4e9bb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface Configuration : NSObject | |
+ (NSString *)name; | |
+ (void)setName:(NSString *)value; | |
+ (BOOL)setting; | |
+ (void)setSetting:(BOOL)value; | |
+ (void)synchronize; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation Configuration | |
static NSString *CONFIGURATION_NAME = @"Configuration.Name"; | |
+ (NSString *)name { | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
[userDefaults registerDefaults:@{CONFIGURATION_NAME : @"No Name"}]; | |
return [userDefaults objectForKey:CONFIGURATION_NAME]; | |
} | |
+ (void)setName:(NSString *)value { | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
[userDefaults setObject:value forKey:CONFIGURATION_NAME]; | |
} | |
static NSString *CONFIGURATION_SETTING = @"Configuration.Setting"; | |
+ (BOOL)setting { | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
[userDefaults registerDefaults:@{CONFIGURATION_SETTING : @(YES)}]; | |
return [userDefaults boolForKey:CONFIGURATION_SETTING]; | |
} | |
+ (void)setSetting:(BOOL)value { | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
[userDefaults setBool:value forKey:CONFIGURATION_SETTING]; | |
} | |
+ (void)synchronize { | |
[[NSUserDefaults standardUserDefaults] synchronize]; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 設定情報を保存 | |
[Configuration setName:@"Hoge"]; | |
[Configuration setSetting:YES]; | |
[Configuration synchronize]; | |
// 設定情報の読み込み | |
NSString *name = [Configuration name]; | |
BOOL setting = [Configuration setting]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment