# userDefualt not saving array

**URL:** <https://forums.kodeco.com/t/userdefualt-not-saving-array/33718>\
**Category:** Swift\
**Created:** [August 25, 2017, 8:46pm UTC](https://forums.kodeco.com/t/userdefualt-not-saving-array/33718 "2017-08-25T20:46:35Z")\
**Posts on this page:** 8\
**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:** [August 25, 2017, 8:46pm UTC](https://forums.kodeco.com/t/userdefualt-not-saving-array/33718/1 "2017-08-25T20:46:35Z")

</div>

My Code below adds another entry to the array if the button jux is pressed. However the new entry is not being saved. The entries that are already their are saved but I can get any new entries to be saved.

VC1

```
          @IBAction func jux(_ sender: Any) {
        if let nextvc2 = self.storyboard?.instantiateViewController(withIdentifier: "JUDO") as? judoScores {

                nextvc2.loneScore = String(dx)}

}

```

VC2

```
     import UIKit

class golfCourseScores: UIViewController {
var array = ["horse", "cow", "camel", "sheep", "goat"]

    public var loneScore: String?

var myArray = String()

@IBOutlet var numbers: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()

}
@IBAction func jux(_ sender: Any) {
    numbers.text = String(describing: array)

    array.append(loneScore!)
    UserDefaults.standard.set(array, forKey: "numbersWithCreationDates")
    UserDefaults.standard.synchronize()

} }

```

---

<div class="post-metadata">

**Author:** ![syedfa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/syedfa/32/101500_2.png) [@syedfa](https://forums.kodeco.com/u/syedfa)\
**Post date:** [August 25, 2017, 9:37pm UTC](https://forums.kodeco.com/t/userdefualt-not-saving-array/33718/2 "2017-08-25T21:37:04Z")

</div>

@timswift. Thanks very much for your question. In looking at your code, I am not seeing where you are assigning a value to the String property, “loneScore”. I do see you declaring it, but I don’t see where a value is being assigned to it. However, you do add this value to your array, which you then store in your UserDefaults. I’m guessing the reason why you don’t see anything saved to your array is because the object you’re adding (i.e. loneScore) has no value.

I hope this helps. 🙂

All the best!

---

<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:** [August 25, 2017, 9:49pm UTC](https://forums.kodeco.com/t/userdefualt-not-saving-array/33718/3 "2017-08-25T21:49:52Z")

</div>

Thanks for the reply! lonescore is being segue from another vc to this vc. Thats why it has no value. Thanks.

---

<div class="post-metadata">

**Author:** ![syedfa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/syedfa/32/101500_2.png) [@syedfa](https://forums.kodeco.com/u/syedfa)\
**Post date:** [August 25, 2017, 9:52pm UTC](https://forums.kodeco.com/t/userdefualt-not-saving-array/33718/4 "2017-08-25T21:52:28Z")

</div>

@timswift If it’s being sent from another VC, I still don’t see where the value of loneScore is being updated. Where in the current VC is this value being updated?

---

<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:** [August 25, 2017, 10:00pm UTC](https://forums.kodeco.com/t/userdefualt-not-saving-array/33718/5 "2017-08-25T22:00:35Z")

</div>

I just updated my question. With 2 VCs hope that helps. The problem is whatever is being segued is not saving in the 2vc.

---

<div class="post-metadata">

**Author:** ![syedfa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/syedfa/32/101500_2.png) [@syedfa](https://forums.kodeco.com/u/syedfa)\
**Post date:** [August 25, 2017, 10:02pm UTC](https://forums.kodeco.com/t/userdefualt-not-saving-array/33718/6 "2017-08-25T22:02:59Z")

</div>

@timswift Can you print out the value of loneScore from your viewDIdLoad method and confirm a value is being received?

---

<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:** [August 25, 2017, 10:19pm UTC](https://forums.kodeco.com/t/userdefualt-not-saving-array/33718/7 "2017-08-25T22:19:08Z")

</div>

The value is only being received if i hit the action button twice with the code in it.

---

<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:** [February 8, 2018, 12:46pm UTC](https://forums.kodeco.com/t/userdefualt-not-saving-array/33718/8 "2018-02-08T12:46:36Z")

</div>

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