但是当它运行时我遇到了崩溃.这是正确的方法,还是有其他方法可以解决这个问题?
工作表类
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对象您应该使用 Codable在UserDefaults中保存非属性列表对象,如下所示.斯威夫特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 – 保存自定义对象数组全部内容,希望文章能够帮你解决ios – 保存自定义对象数组所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)