I want to display on "Profile" cell the image of the user. To do that I supposed to use the same snippet that solve the profile load.
First of all I declared an UIImageView!
variable:
var accountimage: UIImageView!
Then, in my cellForRowAtIndexPath
I downloaded by UserDefaults
constructor the Avatar:
if indexPath.row == 1 {
cell.backgroundColor? = UIColor(red: 236/255,green: 239/255,blue: 240/255,alpha: 1)
let myFont: UIFont? = UIFont(name: "Avenir Next Condensed", size: 22.0)
if let aFont = myFont {
cell.textLabel?.font = aFont
//load account image
if UserDefaults.standard.object(forKey: "profile_avatar") != nil{
let decoded = UserDefaults.standard.object(forKey: "profile_avatar") as! Data
let decodedTeams = NSKeyedUnarchiver.unarchiveObject(with: decoded)
accountimage.image = decodedTeams as? UIImage
cell.imageView?.image = accountimage.image
} else
{
accountimage.image = #imageLiteral(resourceName: "user.png")
cell.imageView?.image = accountimage.image
}
}
}
Debugger says this on accountimage.image = decodedTeams as? UIImage
line:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Edit
Here a screen of my Main.Storyboard
in case should be helpful:
Maybe you made a small mistake on this code
let decoded = UserDefaults.standard.object(forKey: "profile_avatar") as! Data
**as!**means forced conversion but may fail.