EXC_BAD_INSTRUCTION when extending UIView
Question
I'm trying to create my own custom view class for a keyboard I'm playing around with for iOS8 and when I create a new class that extends UIView
I crash with the Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0)
error when the loading the keyboard. The error is shown on the line class KeyButton: UIView {
, and I'm struggling to debug this much further. I was originally trying to extend UIButton
but that didn't work so I changed it to UIView
with no further luck.
I know there will be a few teething issues with Swift/iOS8, but hopefully this is fixable!
Edit
Full class code:
import UIKit
class KeyButton: UIView { /* This is where the app crashes, apparently */
init(frame: CGRect) {
super.init(frame: frame)
// Initialization code
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect)
{
// Drawing code
}
*/
}
Solution
I had a similar issue recently, but figured it out. As it turns out, in iOS 8, but not iOS 7, initWithCoder
(in Objective-C) is required. All you have to do is add the following to your class:
init(coder aDecoder: NSCoder!)
{
super.init(coder: aDecoder)
}
I hope that helps!