티스토리 뷰

1. NSUserDefaults Instance 얻기

+ standardUserDefaults

Returns the shared defaults object.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

+ resetStandardUserDefaults

Synchronizes any changes made to the shared user defaults object and releases it from memory.

Example

[NSUserDefaults resetStandardUserDefaults];

2. NSUserDefaults Object 초기화 하기

- init

Returns an NSUserDefaults object initialized with the defaults for the current user account.

Example

NSUserDefaults *userDefaults = [[NSUserDefaults alloc]init];

3. Registering Defaults

- registerDefaults

Adds the contents of the specified dictionary to the registration domain.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults registerDefaults:@{@"User": @"Raj"}];
 NSLog(@"%@",[userDefaults objectForKey:@"User"]);

Output

2014-04-16 08:19:11.310 iOS-Tutorial[2688:a0b] Raj

4. 키로 값 저장하기

- setBool:forKey

Sets the value of the specified default key to the specified Boolean value.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:YES forKey:@"iOS"];
NSLog(@"%d",[userDefaults boolForKey:@"iOS"]);

Output

2014-04-16 08:22:29.958 iOS-Tutorial[2719:a0b] 1

- setFloat:forKey

Sets the value of the specified default key to the specified floating-point value.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setFloat:12.3 forKey:@"iOS"];
NSLog(@"%f",[userDefaults floatForKey:@"iOS"]);

Output

2014-04-16 08:31:59.136 iOS-Tutorial[2784:a0b] 12.300000

- setInteger:forKey

Sets the value of the specified default key to the specified integer value.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:12 forKey:@"iOS"];
NSLog(@"%d",[userDefaults integerForKey:@"iOS"]);

Output

2014-04-16 08:33:25.364 iOS-Tutorial[2796:a0b] 12

- setObject:forKey

Sets the value of the specified default key in the standard application domain.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@[@"Eezy", @"Tut"] forKey:@"iOS"];
NSLog(@"%@",[userDefaults arrayForKey:@"iOS"]);

Output

2014-04-16 08:21:02.083 iOS-Tutorial[2706:a0b] (
    Eezy,
    Tut
)

- setDouble:forKey

Sets the value of the specified default key to the double value.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setDouble:12.5 forKey:@"iOS"];
NSLog(@"%f",[userDefaults doubleForKey:@"iOS"]);

Output

2014-04-16 08:34:31.684 iOS-Tutorial[2809:a0b] 12.500000

- setURL:forKey

Sets the value of the specified default key to the specified URL.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setURL:[NSURL URLWithString:@"http://ios.eezytutorials.com"] forKey:@"iOS"];
NSLog(@"%@",[userDefaults URLForKey:@"iOS"]);

Output

2014-04-16 08:36:34.433 iOS-Tutorial[2824:a0b] http://ios.eezytutorials.com

5. 저장된 값 키로 불러오기

- arrayForKey

Returns the array associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@[@"Eezy", @"Tut"] forKey:@"iOS"];
NSLog(@"%@",[userDefaults arrayForKey:@"iOS"]);

Output

2014-04-16 08:21:02.083 iOS-Tutorial[2706:a0b] (
    Eezy,
    Tut
)

- boolForKey

Returns the Boolean value associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:YES forKey:@"iOS"];
NSLog(@"%d",[userDefaults boolForKey:@"iOS"]);

Output

2014-04-16 08:22:29.958 iOS-Tutorial[2719:a0b] 1

- dataForKey

Returns the data object associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[@"Eezy" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"iOS"];
NSLog(@"%@",[userDefaults dataForKey:@"iOS"]);

Output

2014-04-16 08:30:11.560 iOS-Tutorial[2744:a0b] <45657a79>

- dictionaryForKey

Returns the dictionary object associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@{@"Eezy" : @"Website"} forKey:@"iOS"];
NSLog(@"%@",[userDefaults dictionaryForKey:@"iOS"]);

Output

2014-04-16 08:31:16.297 iOS-Tutorial[2774:a0b] {
    Eezy = Website;
}

- floatForKey

Returns the floating-point value associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setFloat:12.3 forKey:@"iOS"];
NSLog(@"%f",[userDefaults floatForKey:@"iOS"]);

Output

2014-04-16 08:31:59.136 iOS-Tutorial[2784:a0b] 12.300000

- integerForKey

Returns the integer value associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:12 forKey:@"iOS"];
NSLog(@"%d",[userDefaults integerForKey:@"iOS"]);

Output

2014-04-16 08:33:25.364 iOS-Tutorial[2796:a0b] 12

- objectForKey

Returns the object associated with the first occurrence of the specified default.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@[@"Eezy", @"Tut"] forKey:@"iOS"];
NSLog(@"%@",[userDefaults arrayForKey:@"iOS"]);

Output

2014-04-16 08:21:02.083 iOS-Tutorial[2706:a0b] (
    Eezy,
    Tut
)

- stringArrayForKey

Returns the array of strings associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@[@"Eezy",@"Tuts"] forKey:@"iOS"];
NSLog(@"%@",[userDefaults stringArrayForKey:@"iOS"]);

Output

2014-04-16 08:37:55.046 iOS-Tutorial[2841:a0b] (
    Eezy,
    Tuts
)

- stringForKey

Returns the string associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@"Eezy"forKey:@"iOS"];
NSLog(@"%@",[userDefaults stringForKey:@"iOS"]);

Output

2014-04-16 08:38:31.790 iOS-Tutorial[2866:a0b] Eezy

- doubleForKey

Returns the double value associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setDouble:12.5 forKey:@"iOS"];
NSLog(@"%f",[userDefaults doubleForKey:@"iOS"]);

Output

2014-04-16 08:34:31.684 iOS-Tutorial[2809:a0b] 12.500000

- URLForKey

Returns the NSURL instance associated with the specified key.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setURL:[NSURL URLWithString:@"http://ios.eezytutorials.com"] forKey:@"iOS"];
NSLog(@"%@",[userDefaults URLForKey:@"iOS"]);

Output

2014-04-16 08:36:34.433 iOS-Tutorial[2824:a0b] http://ios.eezytutorials.com

6. 키로 값 삭제하기

- removeObjectForKey

Removes the value of the specified default key in the standard application domain.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@"Eezy"forKey:@"iOS"];
[userDefaults removeObjectForKey:@"iOS"];
NSLog(@"%@",[userDefaults stringForKey:@"iOS"]);

Output

2014-04-16 08:39:12.559 iOS-Tutorial[2876:a0b] (null)

7. Maintaining Persistent Domains

- synchronize

Writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk.

Example

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@"Eezy"forKey:@"iOS"];
NSLog(@"%d",[userDefaults synchronize]);

Output

2014-04-16 08:40:10.188 iOS-Tutorial[2900:a0b] 1
댓글