Hacking with Swift — Challenge 12

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
let picture = pictures[indexPath.row]
cell.textLabel?.text = picture
cell.detailTextLabel?.text = "Viewed \(pictDict[picture]!) times."

return cell
}
func save() {
let jsonEncoder = JSONEncoder()
if let savedData = try? jsonEncoder.encode(pictDict),
let savedPictures = try? jsonEncoder.encode(pictures) {
let defaults = UserDefaults.standard
defaults.set(savedData, forKey: "pictDict")
defaults.set(savedPictures, forKey: "pictures")
} else {
print("Failed to save data.")
}
}
let picture = pictures[indexPath.row]
pictDict[picture]! += 1
save()
tableView.reloadData()
if score > highScore {
save()
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store