# UserDefaults not working when switching classes (Swift 4)

**URL:** <https://forums.kodeco.com/t/userdefaults-not-working-when-switching-classes-swift-4/36695>\
**Category:** Swift\
**Created:** [November 19, 2017, 2:01pm UTC](https://forums.kodeco.com/t/userdefaults-not-working-when-switching-classes-swift-4/36695 "2017-11-19T14:01:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [November 19, 2017, 2:01pm UTC](https://forums.kodeco.com/t/userdefaults-not-working-when-switching-classes-swift-4/36695/1 "2017-11-19T14:01:05Z")

</div>

My code right now takes new entries into its array. When the user hits the home button on the simulator and returns to the original view controller it works perfectly. However when the view controller is sugued to another vc and returns to the original vc and enters a new entry all of the prior entries are deleted and saved over. Basically the problem is when the user switches classes the and returns the userDefault is overwritten by a new userdefault.

```
                     import UIKit
    class ViewController: UIViewController {
   var persons:[Person] = [Person]()
 @IBOutlet var txt: UITextField!
@IBOutlet var label: UILabel!
@IBAction func add(_ sender: Any) {
    let judo = Person.init(name: txt.text! )
self.persons.append(judo)
label.text = String(describing: persons)
UserDefaults.standard.set(label.text, forKey: "name")
      }
           override func viewDidLoad() {
    if let name = UserDefaults.standard.value(forKey: "name") as? String {
        label.text = name
    }}
struct Person : CustomStringConvertible {
    var name: String
    static var myStruct = [String]();
    var description: String {
        return "\(name)"
    }}
    }

```

---

<div class="post-metadata">

**Author:** ![steveb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/steveb/32/94142_2.png) [@steveb](https://forums.kodeco.com/u/steveb)\
**Post date:** [November 20, 2017, 3:09pm UTC](https://forums.kodeco.com/t/userdefaults-not-working-when-switching-classes-swift-4/36695/2 "2017-11-20T15:09:35Z")

</div>

When you write to `UserDefaults` the changes aren’t saved until you call `synchronize()`

So try this:

```auto
UserDefaults.standard.set(label.text, forKey: "name")
UserDefaults.standard.synchronize()

```

---

<div class="post-metadata">

**Author:** ![jerbeers](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jerbeers/32/100211_2.png) [@jerbeers](https://forums.kodeco.com/u/jerbeers)\
**Post date:** [November 27, 2017, 4:54pm UTC](https://forums.kodeco.com/t/userdefaults-not-working-when-switching-classes-swift-4/36695/3 "2017-11-27T16:54:09Z")

</div>

No, if you look at the documentation for `synchronize`, you see that you rarely have to call it manually. @timswift, I’m not sure from your code snippet what is happening, but you can set breakpoints on all the places that call `set` on `UserDefaults` and see if the order is what you expect.

---

<div class="post-metadata">

**Author:** ![steveb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/steveb/32/94142_2.png) [@steveb](https://forums.kodeco.com/u/steveb)\
**Post date:** [November 28, 2017, 12:40pm UTC](https://forums.kodeco.com/t/userdefaults-not-working-when-switching-classes-swift-4/36695/4 "2017-11-28T12:40:24Z")

</div>

@jarbeers I stand corrected. ‘this method is unnecessary’ - I guess maybe it was once, I’ve been calling it for years!

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [May 5, 2018, 6:01am UTC](https://forums.kodeco.com/t/userdefaults-not-working-when-switching-classes-swift-4/36695/5 "2018-05-05T06:01:05Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
