Userdefualt is not applying to struct

I would like whatever is in the textfield when the user hits the button to be added to the struct. The problem is that on the twoViewcontrolelr only one entry is being displayed at a time. I am trying to create a list where every new entry is displayed on a new line. Right now 1 entry (The most recent entry) is being dispalyed on 1 line.


   import UIKit

class ViewController: UIViewController {

@IBOutlet var t: UITextField!

@IBAction func segue(_ sender: Any) { = [String(describing: t.text)+"\n"]

    let defaults = UserDefaults.standard
    defaults.set(, forKey: "SavedStringArray")
struct sam {
static var mm = [String]()}


import UIKit

class twoViewController: UIViewController {
 @IBOutlet var label: UILabel!
override func viewDidLoad() {

    label.text = String(describing:
    let defaults = UserDefaults.standard
    defaults.set(label.text, forKey: "SavedStringArray")


Each time you set the SavedStringArray default, you are overwriting any existing data. If you want to build up a list then you need to get the array of values first and then append the text field content to that array

how do I turn a static var string into a static var array; struct sam {
static var mm = Array} does not work.

Please could you explain the purpose of your sam struct?

sam struct is just where to place whatever was entered in the textfield. So if a then b are both entered sepearatly the struct should hold a, b

You’re overwriting the mm property each time you add text. You probably want to start by using the insert(...) function to append contents to mm rather than resetting it

Thats it. Use the insert function

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