利用NSUserDefaults存取用戶設定

要是程序的設定不多,除了用檔案,我們還可以利用 NSUserDefaults!

假設我們有一個聲音設定:

static NSString *KeySound = @"CubicManSoundKey";

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

NSNumber *defaultValue = [NSNumber numberWithInt:1];
NSDictionary *resourceDict = [NSDictionary dictionaryWithObject:defaultValue forKey:KeySound];
[[NSUserDefaults standardUserDefaults] registerDefaults:resourceDict];

提取現有數值:

int GetSoundOn()
{
return [[NSUserDefaults standardUserDefaults] integerForKey:KeySound];
}

更新數值:

void SetSoundFlag(int flag)
{
[[NSUserDefaults standardUserDefaults] setInteger:flag forKey:KeySound];
}


返回:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License