4

I'm currently working on a calendar within my application I'm writing using Swift. Although it works correctly in that data (event logs) can be entered and added to an array which will show up within a table view, once the application is closed down and removed from the background, when I re-open it, the event logs are gone.

Have I made any 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()
}

Incase it's necessary here's the code which allows data to be entered in 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've tried using the simulator and a real iOS devise along with re-installing Xcode, because I've read that it may be a problem with Xcode itself. I've also tried adding UserDefaults.standard.synchronize() all over the place, however, nothing seems to work.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Lnskip
  • 73
  • 5
  • Your line marked `//gets` doesn't actually DO anything? You are not putting that value anywhere. Is that why you think it isn't saving anything? Because you're not actually getting the saved value out? It should prob be `event = ` – Fogmeister Jan 02 '18 at 23:16

2 Answers2

3

The only line in your code that gets the value out of the UserDefaults is...

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

This doesn't actually do anything with that value. It gets it... and then throws it away.

This will make it look like it isn't being saved.

Change it to ...

event = UserDefaults.standard.object(forKey: "event")
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
-1

You need to call synchronize() after you call set, in order to save the changes

UserDefaults.standard.synchronize()
Abel C
  • 62
  • 4
  • I've tried adding that in numerous times. That doesn't seem to be the issue. I've also read that it's also not necessary. – Lnskip Jan 02 '18 at 23:07
  • TIL: synchronize is not necessary (it is written in the docs for the function). Well... that goes against everything I know about it. LOL! – Fogmeister Jan 02 '18 at 23:13
  • I've just tried adding it to absolutely everywhere I call UserDefaults.standard.set, and it didn't help. Any more suggestions? – Lnskip Jan 02 '18 at 23:14
  • @AbelC Please see https://stackoverflow.com/questions/40808072/when-and-why-should-you-use-nsuserdefaultss-synchronize-method – rmaddy Jan 03 '18 at 00:40