Bigfoot11
Bigfoot11

Reputation: 921

Problems debugging initializer?

I have a debugging error on the following code:

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Here is all of my code:

class ViewController: UIViewController {

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
    super.viewDidLoad()

    var button = UIButton.buttonWithType(.Custom) as UIButton
    button.frame = CGRectMake(160, 100, 50, 50)
    button.layer.cornerRadius = 0.5 * button.bounds.size.width
    button.setImage(UIImage(named:"thumbsUp.png"), forState: .Normal)
    button.addTarget(self, action: "thumbsUpButtonPressed", forControlEvents: .TouchUpInside)
    view.addSubview(button)
}

func thumbsUpButtonPressed() {
    println("thumbs up button pressed")
}

The error says:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)

The debugger output says the following:

fatal error: init(coder:) has not been implemented: file...hi/ViewController.swift, line 14

I have had this error before and am unsure of how to fix it. The weird thing is, there was an error saying You don't have any initializers and had me put it in. It is giving me an error either way (with it or without it).

Any input and suggestions would be greatly appreciated.

Thanks in advance.

Upvotes: 1

Views: 275

Answers (1)

Eric Aya
Eric Aya

Reputation: 70094

In your code, you trigger a "fatalError" instead of initializing. Replace

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

with

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

Upvotes: 1

Related Questions