Objective-C (iOS): データを記憶する

iOSシミュレータのスクリーンショット 2013.01.03 21.58.13

Objective-C: NSUserDefaultsViewController.h

#import <UIKit/UIKit.h>

@interface NSUserDefaultsViewController : UIViewController

@end

Objective-C: NSUserDefaultsViewController.m

#import "NSUserDefaultsViewController.h"

@interface NSUserDefaultsViewController ()

@end

@implementation NSUserDefaultsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton *readButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [readButton setFrame:CGRectMake(0, 0, 100, 50)];
    [readButton setTitle:@"Read" forState:UIControlStateNormal];
    [readButton addTarget:self action:@selector(readValue) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:readButton];
    
    UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [saveButton setFrame:CGRectMake(110, 0, 100, 50)];
    [saveButton setTitle:@"Save" forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(saveValue) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveButton];
    
    UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [removeButton setFrame:CGRectMake(220, 0, 100, 50)];
    [removeButton setTitle:@"Remove" forState:UIControlStateNormal];
    [removeButton addTarget:self action:@selector(removeValue) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:removeButton];
    
    [self saveValue];
}

- (void) saveValue {
    // NSArray, NSDictionary, NSString, NSNumber などとその Mutable 系が入れられる
    [NSUserDefaultsViewController setValueToUserDefault:@"name" value:@"ジョブス"];
    [NSUserDefaultsViewController setValueToUserDefault:@"tool" value:@[@"Xcode", @"Instruments"]];
}

- (void) readValue {
    // Mutable 系をセーブしていても読み込み時は全て Mutable ではなくなる
    NSLog(@"%@", [NSUserDefaultsViewController getValueFromUserDefault:@"name"]);
    NSLog(@"%@", [[NSUserDefaultsViewController getValueFromUserDefault:@"tool"] description]);
}

- (void) removeValue {
    [NSUserDefaultsViewController removeValueFromUserDefault:@"name"];
    [NSUserDefaultsViewController removeValueFromUserDefault:@"tool"];
}

+ (void) setValueToUserDefault:(NSString *)key value:(id)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:value forKey:key];
    [defaults synchronize];
}

+ (void) removeValueFromUserDefault:(NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:key];
    [defaults synchronize];
}

+ (id) getValueFromUserDefault:(NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    id value = [defaults objectForKey:key];
    if((value != nil) && (![[NSNull null] isEqual:value])) {
        return value;
    }
    return nil;
}

@end

Output

2013-01-03 21:58:00.559 Snippets[7813:c07] ジョブス
2013-01-03 21:58:00.561 Snippets[7813:c07] (
    Xcode,
    Instruments
)
2013-01-03 21:58:05.127 Snippets[7813:c07] ジョブス
2013-01-03 21:58:05.128 Snippets[7813:c07] (
    Xcode,
    Instruments
)
2013-01-03 21:58:08.143 Snippets[7813:c07] (null)
2013-01-03 21:58:08.144 Snippets[7813:c07] (null)