standardUserDefaults 有什么用?
我是 iPhone 编程新手。请解释一下这段代码。这就是standardUserDefaults 的使用?这段代码将如何工作?
-(void)load
{
DLog("Load Configuration");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool savedDefaults = [defaults boolForKey:kKeySavedDefaults];
if (savedDefaults)
{
columns = [defaults integerForKey:kKeyColumns];
if (columns == 0) columns = kColumnsDefault;
rows = [defaults integerForKey:kKeyRows];
if (rows == 0) rows = kRowsDefault;
photoType = [defaults integerForKey:kKeylastPhotoType];
photoEnabled = [defaults boolForKey:kKeyPhotoEnabled];
numbersEnabled = [defaults boolForKey:kKeyNumbersEnabled];
soundEnabled = [defaults boolForKey:kKeySoundEnabled];
}
else
{
columns = kColumnsDefault;
rows = kRowsDefault;
photoType = klastPhotoTypeDefault;
photoEnabled = kPhotoEnabledDefault;
numbersEnabled = kNumbersEnabledDefault;
soundEnabled = kSoundEnabledDefault;
}
}
-(void)save
{
DLog("Save Configuration");
BOOL restart = NO;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults integerForKey:kKeyColumns] != columns) { restart = YES; }
if ([defaults integerForKey:kKeyRows] != rows) { restart = YES; }
[defaults setBool:YES forKey:kKeySavedDefaults];
[defaults setInteger:columns forKey:kKeyColumns];
[defaults setInteger:rows forKey:kKeyRows];
[defaults setInteger:photoType forKey:kKeylastPhotoType];
[defaults setBool:photoEnabled forKey:kKeyPhotoEnabled];
[defaults setBool:numbersEnabled forKey:kKeyNumbersEnabled];
[defaults setBool:soundEnabled forKey:kKeySoundEnabled];
[defaults synchronize];
[board configChanged:restart];
}
I am new in iphone programming. please explain me this code. and that is the use of standardUserDefaults? how this code will work?
-(void)load
{
DLog("Load Configuration");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool savedDefaults = [defaults boolForKey:kKeySavedDefaults];
if (savedDefaults)
{
columns = [defaults integerForKey:kKeyColumns];
if (columns == 0) columns = kColumnsDefault;
rows = [defaults integerForKey:kKeyRows];
if (rows == 0) rows = kRowsDefault;
photoType = [defaults integerForKey:kKeylastPhotoType];
photoEnabled = [defaults boolForKey:kKeyPhotoEnabled];
numbersEnabled = [defaults boolForKey:kKeyNumbersEnabled];
soundEnabled = [defaults boolForKey:kKeySoundEnabled];
}
else
{
columns = kColumnsDefault;
rows = kRowsDefault;
photoType = klastPhotoTypeDefault;
photoEnabled = kPhotoEnabledDefault;
numbersEnabled = kNumbersEnabledDefault;
soundEnabled = kSoundEnabledDefault;
}
}
-(void)save
{
DLog("Save Configuration");
BOOL restart = NO;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults integerForKey:kKeyColumns] != columns) { restart = YES; }
if ([defaults integerForKey:kKeyRows] != rows) { restart = YES; }
[defaults setBool:YES forKey:kKeySavedDefaults];
[defaults setInteger:columns forKey:kKeyColumns];
[defaults setInteger:rows forKey:kKeyRows];
[defaults setInteger:photoType forKey:kKeylastPhotoType];
[defaults setBool:photoEnabled forKey:kKeyPhotoEnabled];
[defaults setBool:numbersEnabled forKey:kKeyNumbersEnabled];
[defaults setBool:soundEnabled forKey:kKeySoundEnabled];
[defaults synchronize];
[board configChanged:restart];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用户默认值用于存储少量配置参数。
来自文档:
User defaults are used to store little configuration parameters.
From the documentation :
如果你想存储一些少量的数据,例如用户名,密码......通过使用这些概念,你可以存储值
If you want to store some small amount of data like username,password...... by using these concept you can store the values