0

My app is crashing, but there is no additional information as to why. When I go to a certain view controller, it requires loading a custom UIView from a nib. The line to load the nib is where I am getting the crash.

I get an error message of

Thread 1: EXC_BAD_ACCESS (code=2, address=0x16b547db8)

but there is no output to the console or further information.

Scheme is set to debug. I've tried intentionally making an error in the spelling of the nib name, and that gives me all of the necessary debug information in the console.

The line causing the crash is:

let v = Bundle.main.loadNibNamed("HelpPageSection", owner: self, options: nil)![0] as! HelpPageSection
Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24
mightknow
  • 256
  • 3
  • 14

2 Answers2

0

To load an xib:-

let v = Bundle.main.loadNibNamed("HelpPageSection", owner: self, options: nil)?.first as! HelpPageSection

It helps.

steveSarsawa
  • 1,559
  • 2
  • 14
  • 31
0

I added some comments to print when the initializer was being called, and it was indeed an infinite loop. However, there were still issues. I tried a slightly different way of loading the nib and it works. I connected the base view of the xib file as an IBOutlet, then used this code:

@IBOutlet var main: UIView!
func commonInit() {
    Bundle.main.loadNibNamed("HelpPageSection", owner: self, options: nil)
    self.addSubview(self.main)
}
override init(frame: CGRect) {
    super.init(frame: frame)
    self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.commonInit()
}
mightknow
  • 256
  • 3
  • 14
  • It does, however, seem strange to me that the nib loading line appears to be an unused result, and that the adding of the subview is not necessarily related to it. I must be misunderstanding how the nib loading function works... – mightknow May 01 '19 at 03:14