"Ambiguous reference to the init (...) element" invoking the underlying initializer

I have a base class:

class ViewController: UIViewController
{
    init(nibName nibNameOrNil: String?)
    {
        super.init(nibName: nibNameOrNil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) { }
}

subclass:

class OneViewController: ViewController
{
    private var one: One
    init(one: One)
    {
        self.one = one

        super.init(nibName: "OneNib")
    }

    required init?(coder aDecoder: NSCoder) { }
}

and a subclass of the specified subclass:

class TwoViewController: OneViewController
{
    private var two: Two
    init(two: Two)
    {
        self.two = two

        super.init(nibName: "TwoNib") <== ERROR
    }

    required init?(coder aDecoder: NSCoder) { }
}

In the specified line, I get an error message:

Ambiguous reference to member 'init(one:)'

I don’t understand why the compiler cannot understand what I mean ViewController init(), how it was done in One init().

What am I missing and / or wrong?

+4
source share
3 answers

I don’t understand why the compiler cannot understand what I mean ViewController init(), how I managed to do it in Oneinit()

This is because, from within the TwoViewController, the compiler does not see the ViewController init. Rule:

, .

, OneViewController , init(one:). , super, TwoViewController.

, ViewController init(nibName:) OneViewController, OneViewController ( , , super).

+5

Swift " " , OneViewController self.one, init(one: One). , .

  • , , , two.one ?

    let two = Two(two: SomeTwo)
    print(two.one)
    
  • - undefined. .

One.init(one:), , .

+2

you do not need to pass the value to the ViewController constructor. you can define a public object in oneViewController and access the external.

 class OneViewController: ViewController {
    public var one: One

 }


let vc = storyboard?.instantiateViewControllerWithIdentifier("OneViewController") as OneViewController

let one = One()
vc.one = one
+1
source

Source: https://habr.com/ru/post/1652606/


All Articles