arrays - Swift UserDefaults returns nil -
trying learn doing todolist; have 2 viewcontrollers, 1 store , 1 display in table view. table returns nil; going wrong ?
storage view:
var todostorage = userdefaults.standard.object(forkey: "todo") var todolist = [string] () @ibaction func savebutton(_ sender: any) { if (todostorage as? [string]) != nil { todolist.append(itemlabel.text!) } else { todolist = [itemlabel.text!] } userdefaults.standard.set(todolist, forkey: "todo") itemlabel.text = "" } display view:
var todo = [userdefaults.standard.object(forkey: "todo")] public func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int{ return todo.count } public func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = uitableviewcell(style: uitableviewcellstyle.default, reuseidentifier: "idcell") cell.textlabel?.text = string(describing: todo[indexpath.row]) return cell }
just use store array of strings
var todo = userdefaults.standard.stringarray(forkey: "todo") ?? [string]()
Comments
Post a Comment