gpt4 book ai didi

ios - 保存自定义对象数组

转载 作者:搜寻专家 更新时间:2023-10-31 08:19:33 25 4
gpt4 key购买 nike

我尝试使用名为 Sheet 的自定义对象保存和检索笔记数据。但是我在运行时遇到了崩溃。这是正确的方法还是有其他方法可以解决这个问题?

工作表类

class Sheet {
var title = ""
var content = ""
}

这是 UITableViewController 的类

class NotesListTableVC: UITableViewController {

var notes = [Sheet]()

override func viewDidLoad() {
super.viewDidLoad()

if let newNotes = UserDefaults.standard.object(forKey: "notes") as? [Sheet] {
//set the instance variable to the newNotes variable
notes = newNotes
}
}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return notes.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "notesCELL", for: indexPath)

cell.textLabel!.text = notes[indexPath.row].title

return cell
}

// Add new note or opening existing note
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editNote" {
var noteContentVC = segue.destination as! NoteContentVC
var selectedIndexPath = tableView.indexPathForSelectedRow
noteContentVC.note = notes[selectedIndexPath!.row]
}
else if segue.identifier == "newNote" {
var newEntry = Sheet()
notes.append(newEntry)
var noteContentVC = segue.destination as! NoteContentVC
noteContentVC.note = newEntry
}
saveNotesArray()
}

// Reload the table view
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.tableView.reloadData()
}

// Deleting notes
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
notes.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}

// Save the notes
func saveNotesArray() {
// Save the newly updated array
UserDefaults.standard.set(notes, forKey: "notes")
UserDefaults.standard.synchronize()
}

}

我应该在哪里调用 saveNotesArray 函数?

最佳答案

您正在尝试将一组自定义对象保存到 UserDefaults。您的自定义对象不是 property list object 你应该使用 Codable像这样在 UserDefaults 中保存非属性列表对象。

swift 4

自定义类

class Sheet: Codable {
var title = ""
var content = ""
}

ViewController.swift

class ViewController: UIViewController {

var notes = [Sheet]()

override func viewDidLoad() {
super.viewDidLoad()

getSheets()
addSheets()
getSheets()
}
func getSheets()
{
if let storedObject: Data = UserDefaults.standard.data(forKey: "notes")
{
do
{
notes = try PropertyListDecoder().decode([Sheet].self, from: storedObject)
for note in notes
{
print(note.title)
print(note.content)
}
}
catch
{
print(error.localizedDescription)
}
}
}
func addSheets()
{
let sheet1 = Sheet()
sheet1.title = "title1"
sheet1.content = "content1"

let sheet2 = Sheet()
sheet2.title = "title1"
sheet2.content = "content1"

notes = [sheet1,sheet2]
do
{
UserDefaults.standard.set(try PropertyListEncoder().encode(notes), forKey: "notes")
UserDefaults.standard.synchronize()
}
catch
{
print(error.localizedDescription)
}
}
}

关于ios - 保存自定义对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48503074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com