//Retrieving SwitchValue
NSUserDefaults *SwitchV = [NSUserDefaultsstandardUserDefaults];
IsOnoff = [SwitchV integerForKey:@"integerKey"];//提取現有數值
//Retrieving TextField String
NSUserDefaults *TextFieldV = [NSUserDefaultsstandardUserDefaults];
NSString *TextFieldString = [TextFieldVstringForKey:@"TextFieldString"];
//Save SwitchValue
NSUserDefaults *SwitchV = [NSUserDefaultsstandardUserDefaults];
[SwitchV setInteger:IsOnoffforKey:@"integerKey"];//更新數值
//Save TextField String
NSUserDefaults *TextFieldV = [NSUserDefaultsstandardUserDefaults];
[TextFieldV setObject:theTextField.textforKey:@"TextFieldString"];
我們在程序起始位置,可以先設定一個默認值:
- NSNumber *defaultValue = [NSNumber numberWithInt:1];//This is used for creating a NSNumber from a integer value.
- NSDictionary *resourceDict = [NSDictionary dictionaryWithObject:defaultValue forKey:KeySound];
- [[NSUserDefaults standardUserDefaults] registerDefaults:resourceDict];