2021年4月11日星期日

UserDefaults with Struct - TableView not displaying data saved from UserDefaults?

I am having a problem displaying data on the table view cell These are my code:

struct Person: Codable { var firstName: String var lastName: String }

class viewController: UIViewController {

var newPerson = [Person]()    override func viewDidLoad() {        let person = [Person(firstName: "Peterson", lastName: "Jota") , Person(firstName: "John", lastName: "Paul")]        let encoder = JSONEncoder()    if let encoded = try? encoder.encode(person) {      UserDefaults.standard.set(encoded, forKey: "savedPerson")    }  }  

}

var person = Person

func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return person.count }

func tableView( tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! UITableViewCell    cell.textLabel?.text = person[indexPath.row]    return cell  

}

https://stackoverflow.com/questions/67051802/userdefaults-with-struct-tableview-not-displaying-data-saved-from-userdefaults April 12, 2021 at 10:00AM

没有评论:

发表评论