-1

I've used the next code for adding subview into UIView in the custom cell:

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 36, height: 36))
imageView.image = UIImage(named: "addNewSkill.png")

cell.countView.addSubview(imageView)

where:

@IBOutlet var countView: UIView!

on my custom cell file. When I run my app it crashes with the next error in logs:

fatal error: unexpectedly found nil while unwrapping an Optional value
Printing description of cell.countView:
(UIView!) countView = nil

What is the problem and how can I fix it?

Orkhan Alizade
  • 7,379
  • 14
  • 40
  • 79

2 Answers2

0

You can prevent the crash from happening by safely unwrapping cell.countView with an if let statement.

if let countv = cell.countView {
    countv.addSubview(imageView)
}

You will still have to do some debugging to see why you are getting a nil value there though.

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
0

I guess your problem is here .

enter image description here

aaisataev
  • 1,643
  • 3
  • 22
  • 38