Ambiguous reference to member 'first(where:)' error when referencing child view controller

748 Views Asked by At

I am writing an iOS application and have a child view controller that I am trying to reference. The child view controller is the delegate of the parent.

At the top of the parent view controller, I have: fileprivate var cameraViewController: CameraViewController<AnyObject>?

In viewDidLoad(), I have

guard let cameraController = childViewControllers.first as? CameraViewController else  {
            fatalError("Check storyboard for missing CameraViewController")
        }

        cameraViewController = cameraController

However, I'm getting the error: Ambiguous reference to member 'first(where:)'

Can anyone explain to me why this is happening? I only have one child view controller for the parent.

2

There are 2 best solutions below

0
On BEST ANSWER

CameraViewController needs to be the same everywhere. When I instantiated cameraViewController as a CameraViewController, that needed to be reflected in the second part of the code. The second part should look like this:

guard let cameraController = childViewControllers.first as? CameraViewController<AnyObject> else  {
            fatalError("Check storyboard for missing CameraViewController")
        }

        cameraViewController = cameraController
0
On

Can't reproduce. This is the entire code in my app's single view controller file:

import UIKit
class CameraViewController : UIViewController {}
class ViewController: UIViewController {
    fileprivate var cameraViewController: CameraViewController?
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let cameraController = childViewControllers.first as? CameraViewController else  {
            fatalError("Check storyboard for missing CameraViewController")
        }
        cameraViewController = cameraController
    }
}

It compiles just fine.