Why doesn't UserDefaults.standard.set save my array? After removing the application from the background, the data is cleared

I am currently working on a calendar in my application, which I write using Swift. Although it works correctly that data (event logs) can be entered and added to the array, which will be displayed as a table, as soon as the application is closed and removed from the background, when I open it again, the event logs are gone.

Did I make mistakes in my code?

import UIKit
import JTAppleCalendar
var event = [String]()
var userData = false

class CalendarViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var calendarView: JTAppleCalendarView!
@IBOutlet weak var monthAndYear: UILabel!
@IBOutlet weak var eventTable: UITableView!

override func viewDidAppear(_ animated: Bool) {
    eventTable.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()
    configureCalendarView()

    userData = UserDefaults.standard.bool(forKey: "userData")

    if userData == true {
        UserDefaults.standard.object(forKey: "event")  //gets
    }

    else
    {
        event.append("NO USER DATA")
        UserDefaults.standard.set(event, forKey: "event")

        if event[0] == "NO USER DATA" {
            event.remove(at: 0)
            UserDefaults.standard.set(event, forKey: "event")
        }
    }
    eventTable.reloadData()
        }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return event.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: nil)
    cell.textLabel?.text = event[indexPath.row]
    return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete
    {
        event.remove(at: indexPath.row)
        UserDefaults.standard.set(event, forKey: "event")
    }
    eventTable.reloadData()
}

Add here the code that allows you to enter data into the array:

class AddEventViewController: UIViewController {

@IBOutlet weak var eventText: UITextField!
@IBAction func addEvent(_ sender: Any) {

    userData = true
    UserDefaults.standard.set(userData, forKey: "userData")

    if eventText.text == ""
    {
    }
    else
    {
        event.append(eventText.text!)
        UserDefaults.standard.set(event, forKey: "event")
    }
}

I tried using the simulator and the real iOS program along with reinstalling Xcode, because I read that it could be a problem with Xcode itself. I also tried adding UserDefaults.standard.synchronize () everywhere, however nothing works.

+4
2

, UserDefaults, ...

UserDefaults.standard.object(forKey: "event")

. ... .

, .

...

event = UserDefaults.standard.object(forKey: "event")
+3

synchronize() set,

UserDefaults.standard.synchronize()
-1

Source: https://habr.com/ru/post/1691656/


All Articles