简体   繁体   English

为什么UserDefaults.standard.set不能保存我的数组? 从后台删除应用程序后,数据将被清除

[英]Why isn't UserDefaults.standard.set saving my array? After the application is removed from background the data is cleared

I'm currently working on a calendar within my application I'm writing using Swift. 我正在使用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. 我已经尝试使用模拟器和真正的iOS设备以及重新安装Xcode,因为我已经读过它可能是Xcode本身的一个问题。 I've also tried adding UserDefaults.standard.synchronize() all over the place, however, nothing seems to work. 我也试过在所有地方添加UserDefaults.standard.synchronize(),但似乎没有任何效果。

The only line in your code that gets the value out of the UserDefaults is... 代码中唯一从UserDefaults中获取值的行是......

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

调用set后需要调用synchronize()以保存更改

UserDefaults.standard.synchronize()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM