I am coming from reading code from a ton of StackOverFlow posts for what seems to be a rather simple procedure, but when I try to implement what I've learned in my own code it fails.
In my "ViewController" didSelectRowAt function, I initialized the user defaults
let song = tableViewData[indexPath.section].songData[indexPath.row - 1]
let songImage = song.artwork
UDM.shared.defaults.setValue(song.title, forKey: "name")
UDM.shared.songCover.setValue(songImage, forKey: "cover")
Then created a class to hold the UserDefaults
class UDM{
static let shared = UDM()
//let defaults = UserDefaults(suiteName: com.CTMVenturesInc.MusicTesters.saved.data)
let defaults = UserDefaults()
let songCover = UserDefaults()
//other funcs
}
Following that in my "TealViewController" I created the label & image element
@IBOutlet var label: UILabel!
@IBOutlet var coverImage: UIImageView!
Lastly in the viewDidLoad of TealViewController I set the values
if let value = UDM.shared.defaults.value(forKey: "name") as? String{
label.text = value
}
if let value = UDM.shared.songCover.value(forKey: "cover") as? MPMediaItemArtwork{
coverImage.image = value.image(at: CGSize(width: 400, height: 400))
}
This runs and works perfectly with just the text default, but when I try to include the image code I get this run error
Thread 1: "Attempt to insert non-property list object <MPConcreteMediaItemArtwork: 0x2830c4c80> for key cover"
So I found this post Save images in NSUserDefaults? and tried to implement in my view controller replacing this
UDM.shared.songCover.setValue(songImage, forKey: "cover")
with this
[UDM.shared.songCover.set: UIImagePNGRepresentation(songImage) forKey: "cover"]
But get a cannot find forKey error. I looked at this post How to pass UI image from one view controller to another? and tried this
UDM.shared.songCover.standardUserDefaults().setObject(UIImagePNGRepresentation(songImage), forKey:"cover")}
What is it that I am not putting together here?