How to set initial values for NSUserDefault Keys?

前端 未结 4 1015
轻奢々
轻奢々 2020-12-02 14:30

I want to set some initial values for my NSUserDefault keys so that the first run of the app has some reasonable initial settings. I thought I ran across a simple way to do

4条回答
  •  时光取名叫无心
    2020-12-02 14:51

    If you have many default values, let use ola's answer, otherwise this is good for a few params

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    if (![defaults boolForKey:USERDEFAULT_IS_INITIALIZED]) { 
        [defaults setBool:YES forKey:USERDEFAULT_IS_INITIALIZED];
    
        // Set initial values 
        ...
    
        [defaults synchronize];
    }
    

提交回复
热议问题