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)"
}}
}