Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Must call a designated initializer of the superclass uiinputviewcontroller

I get the error in the subject after this morning's upgrade to 8.3.

The code below used to work perfectly, however it doesn't compile anymore. Can any of you please help me?

protocol CustomAccessoryProtocol {
    func controlButtonPressed(tag:Int)
}

class CustomAccessory : UIInputViewController {
    var accessoryView : UIView!
    var delegate : CustomAccessoryProtocol!

    @IBOutlet weak var returnButton: UIButton!
    @IBOutlet weak var backButton: UIButton!
    @IBOutlet weak var forwardButton: UIButton!

    init(delegate: CustomAccessoryProtocol){
        super.init()
        self.delegate = delegate
    }

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

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        let customNib = UINib(nibName: "CustomAccessory", bundle: nil)
        accessoryView = customNib.instantiateWithOwner(self, options: nil)[0] as! UIView
    }

    @IBAction func buttonPress(sender: AnyObject) {
        delegate.controlButtonPressed(sender.tag!)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(accessoryView)
    }
}
like image 386
Razvan Soneriu Avatar asked Apr 09 '15 06:04

Razvan Soneriu


People also ask

What is designated initializer in Swift?

Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain.

Why we use super init in Swift?

For safety reasons, Swift always makes you call super. init() from child classes – just in case the parent class does some important work when it's created. SPONSORED Get complete control over your app releases with Runway. Ship more confidently from kickoff to rollout with centralized collaboration and automation.

Why do we need init coder NSCoder?

If you subclass from something and need to create a new init , or override an existing init , Xcode will create an error telling you need also need to include required init?(coder aDecoder: NSCoder) . The decoder, NSCoder is related to the the Interface Builder.


1 Answers

I had the same problem on the following code with NSWindowController:

init() {
    super.init()
}

I changed it to:

convenience init() {
    self.init()
}

I'm thinking that Apple is enforcing convenience inits more strictly than before.

like image 159
Joel B Avatar answered Oct 05 '22 23:10

Joel B