So here is the way I solved this challenge. But first, I want to clarify that I set it up so that each image retains its own view count. It looks like you are trying to combine all of the view counts under one key value.
Since we're just saving integers for the number of views, we can just use the UserDefaults class without having to use codable.
Here is what I did:
In DetailViewController file, I created a UserDefaults object and a savedCount property all the way at the top
var defaults = UserDefaults.standard
var savedCount = Int()
Under if let imageToLoad I attempted to load from a key even before saving. This will give me a value of 0 as explained in the lesson:
integer(forKey:) returns an integer if the key existed, or 0 if not
imageView.image = UIImage(named: imageToLoad)
savedCount = defaults.integer(forKey: "\(imageToLoad)")
navigationItem.setTitle(title: selectedImage!, subtitle: "Viewed: \(savedCount)")
savedCount += 1
defaults.set(savedCount, forKey: "\(imageToLoad)")
And regarding navigationItem.setTitle... I used aheze's idea from stackoverflow on how to add a subtitle to the nav bar
I hope this helped!