0

I use this code to create an image:

var book1ImageView: UIImageView!

book1ImageView?.image = UIImage(named:"book.png")
self.view.addSubview(self.book1ImageView)

But I get this error message:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

On the last line: self.view.addSubview(self.book1ImageView)

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
user
  • 83
  • 12
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Cristik Aug 04 '18 at 17:10

1 Answers1

0

You need to init the imageview

book1ImageView = UIImageView(frame:CGRect(x:0,y:20,width:100,height:100))
book1ImageView.image = UIImage(named:"book.png")
self.view.addSubview(self.book1ImageView)
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Thank you! I use `var bookX: CGFloat = 0` in `UIImageView(frame:CGRect(x: bookX,y:20,width:100,height:100))`. But problem was solved then I use `UIImageView(frame:CGRect(x: CGFloat bookX),y:20,width:100,height:100))` – user Aug 04 '18 at 16:33
  • Could you please check this question - https://stackoverflow.com/questions/51679047/in-app-purchase-in-application I do not understand why no one answers. Maybe my question is not clear? Please check. – user Aug 04 '18 at 16:36