iphone--使用NSUserDefaults存储数据

//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"];


我們在程序起始位置,可以先設定一個默認值:

  1. NSNumber *defaultValue = [NSNumber numberWithInt:1];//This is used for creating a NSNumber from a integer value.
  2.     NSDictionary *resourceDict = [NSDictionary dictionaryWithObject:defaultValue forKey:KeySound];
  3.     [[NSUserDefaults standardUserDefaults] registerDefaults:resourceDict];




你可能感兴趣的:(Integer,存储)