title: Is it fine to access NSUserDefaults/UserDefaults frequently?
body: <p>I have a login view controller in which the user enters their preferences like whether or not he wants to activate certain UI features.</p>
  
  <p>I store these as variables whose getters and setters directly access <code>UserDefaults</code>, here is an example of one of these:</p>
  
  <pre><code>class Preferences {
          static var likesSpaghetti : Bool {
              set (likesSpaghetti) {
                  UserDefaults.standard.set(likesSpaghetti, forKey: "likesSpaghetti")
              }
  
              get {
                  return UserDefaults.standard.bool(forKey: "likesSpaghetti")
              }
          }
  }
  </code></pre>
  
  <p>So that whenever I want to set any of these I simply write something like this:</p>
  
  <p><code>Preferences.likesSpaghetti = false</code></p>
  
  <p>Now, my question is: Can I set these variables every time the user flicks the on/off switch or should I keep the preference represented as a local variable and then only set:</p>
  
  <p><code>Preferences.likesSpaghetti = spaghettiSwitch.isOn</code></p>
  
  <p>when the user segue's away from the <code>loginViewController</code>? Is every access of <code>UserDefault</code> instant and quick? or is it laggy and should be used mercifully?</p>
  
  <p><strong>Edit after closing this question:</strong> So I learned to not prematurely optimize, and that it is probably ok within the scope of a few dozen elements. So I should be fine. I'm going to just update every time the user modifies anything so that my code is a lot easier to read and maintain.</p>
  
  <p>Thanks everyone!</p>