To preface, I am new to programming and Swift.
I am following a tutorial and am learning to save user data. Only when the save encoder/decoder functionality is enabled will this crash occur.
Here is the encoding and decoding:
override func encode(with coder: NSCoder) {
coder.encode(clickCount, forKey: "clickCount")
}
required init?(coder: NSCoder) {
super.init(coder: coder)
clickCount = coder.decodeObject(forKey: "clickCount") as? [Int] ?? [Int]()
}
I am trying to increment the array at the indexPath.row of the clickCount array to know how many times a user clicks on a cell in my tableView
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(identifier: "Detail") as? DetailViewController {
vc.selectedImage = pictures[indexPath.row]
vc.totalPictures = pictures.count
vc.selectedPictureNumber = (pictures.firstIndex(of: vc.selectedImage ?? "Error") ?? 0) + 1
clickCount[indexPath.row] += 1
navigationController?.pushViewController(vc, animated: true)
}
}
And finally, here is the clickCount array
var clickCount = [0,0,0,0,0,0,0,0,0,0]
To be clear, incrementing the array works as expected when I comment out the encoder and decoder functionality, but I want to save how many times each row has been clicked between sessions.