0

While trying to implement the cocoa pod, BWWalkthrough, into my current project programmatically, I ran into a problem with the init functions/inheritance.

I tried to subclass BWWalkThroughViewController in my own class GetStartedViewController.

Here is the code from BWWalkThroughViewController which is a subclass of UIViewController:

class BWWalkThroughViewController: UIViewController {
    required public init?(coder aDecoder: NSCoder) {
        // Setup the scrollview
        scrollview.showsHorizontalScrollIndicator = false
        scrollview.showsVerticalScrollIndicator = false
        scrollview.isPagingEnabled = true
        super.init(coder: aDecoder)
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
}

But when I try and subclass BWWalkthroughViewController in my own class I get errors saying "Ambiguous reference to member 'init(coder:)'" when calling super.init(nibName, bundle) and another error (Initializer does not override a designated initializer from its superclass) when I try to override the init(nibname, bundle):

import BWWalkthrough

class GetStartedViewController: BWWalkthroughViewController  {

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

public init(){
    super.init(nibName: nil, bundle: nil) <== ERROR (Ambiguous reference to member 'init(coder:)')
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) <== ERROR (Initializer does not override a designated initializer from its superclass) {
    super.init(nibName: nil, bundle: nil) <== ERROR (Ambiguous reference to member 'init(coder:)')
}
}

I've tried different variations of containing just the public init or just the override init and not both, so I included both to show the error.

Even when I use autocomplete to call super.init, all that shows up is the function with parameters of nscoder rather than with nibName and bundle.

Can someone please help me understand why this error is happening and how to fix it? I don't understand why I can't call super.init(nibName, bundle) or override the function.

Jonathan Wong
  • 460
  • 5
  • 11
  • You are missing the ? in `init?(coder aDecoder: NSCoder)` – Paulw11 Jul 21 '18 at 22:56
  • @Paulw11 Thank you! I added that to my code and edited the post to reflect the changes. Could you help me understand the root of the errors being caused? – Jonathan Wong Jul 21 '18 at 23:02
  • Does the error happen too if you comment out the third initializer? – Fabian Jul 21 '18 at 23:54
  • Try to give super.init a string and a bundle, maybe super has `init(nibName:bundle:)` overloaded or sth? Ok that sounds weird. Or maybe not so weird since you derive from BWWalkthroughViewController and not from a simple UIViewController. – Fabian Jul 22 '18 at 00:00
  • @Purpose The errors still happen if I comment out the third initializer... It's so weird. BWWalkthroughViewController inherits from UIViewController and then implements init(nibName, bundle)... so I don't really understand why I can't then call it from a subclass of BWWalkthroughViewController. Even when I try and call super.init() autocomplete doesn't show the option with nibName or bundle- just nscoder. – Jonathan Wong Jul 22 '18 at 04:39
  • Try to import UIKit – Tal Cohen Jul 22 '18 at 06:23
  • `and then implements init(nibName, bundle)`: Do things work if you give over `String?` and `Bundle?` instead of both times nil? And does BWWalkthroughViewController override `init(nibName:bundle:)` or offers another overload with the same argument names but different types? – Fabian Jul 22 '18 at 07:32

0 Answers0