title: Right way to sync a variable with UserDefaults
body: <p>I would like to keep a variable sync with UserDefaults directly when I set a value. I've done it like this:</p>
  
  <pre><code>var userId: String {
      get {
          return UserDefaults.standard.string(forKey: kUserIdKey) ?? ""
      }
      set {
          UserDefaults.standard.set(newValue, forKey: kUserIdKey)
      }
  }
  </code></pre>
  
  <p>It is working fine but is it the right way to do it?</p>
  
  <p>If so, what about Array?</p>
  
  <pre><code>var tagsList: [Int] {
      get {
          return (UserDefaults.standard.object(forKey: kTagsList) as? [Int]) ?? [Int]()
      }
      set {
          UserDefaults.standard.set(newValue, forKey: kTagsList)
      }
  }
  </code></pre>
  
  <p>If I add an element <code>tagsList.append(0)</code>, is the new value stored in UserDefaults? </p>
  
  <p><strong>Update</strong>. </p>
  
  <p>Here is what I have in the playground:
  <a href="https://i.stack.imgur.com/kmzrr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kmzrr.png" alt="enter image description here"></a></p>