Tiffany Xu
Tiffany Xu

Reputation: 33

unresolved identifier for an image in assets when trying to use it programmatically

I'm trying to learn how to use autolayout programmatically in Swift using this tutorial. I added the PNG to the Assets folder, but when I try to add an UIImageView in the ViewController let imageView = UIImageView(image: bear) I get the error "use of unresolved identifier 'bear'".

However, if I go to the Main.storyboard, I can add the image fine. Any tips on why this image isn't recognized in the ViewController? (I tried this with multiple PNGs in case it was a bad image, but same result).

Upvotes: 1

Views: 620

Answers (1)

jaelee
jaelee

Reputation: 46

bear needs to be a type UIImage, try something like this

func addImage() {
     let imageView = UIImageView()
     let bear = UIImage(named: "bear") // whatever the name of that image file is, within your assets

     imageView.image = bear // setting your bear image here
}

Upvotes: 1

Related Questions