userDefualt not saving array

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()

}  }

@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. :slight_smile:

All the best!

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

@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?

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

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

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

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