function "picker " can't be found and I don't understand why

Hello, below are the lines of code where the error is found. It seems to me that I have declared the function "picker" ( UIDatePicker) however I get an error that "Cannot find 'picker' in scope" ( the error is displayed on line 66, next to " let pickedDatetime = picker.time"

import UIKit

    class DateViewController: UIViewController, UITextFieldDelegate {



        private let field: UITextField = {

              let field = UITextField(frame: CGRect (x: 220, y: 220, width: 100, height: 100))

               field.placeholder = "Enter Text"

            field.becomeFirstResponder()

               return field    

        }()



        private let Buttonb: UIButton = {

            let Buttonb = UIButton(frame: CGRect (x: 157, y: 470, width: 100, height: 150))

            Buttonb.addTarget(self, action: #selector(didTapButtonValidate), for: .touchUpInside)

           Buttonb.setTitle( "save", for: .normal)

            Buttonb.addTarget(self, action: #selector(didTapButtonValidate), for: .touchUpInside)

            return Buttonb

        }()

        private let returnButton = UIButton()

        public var completionHandler: ((String, Date) -> Void)?

        override func viewDidLoad() {

            super.viewDidLoad()

            view.backgroundColor = .systemTeal

            view.addSubview(field)

            view.addSubview(Buttonb)

            field.becomeFirstResponder()

            field.delegate = self

            

            let picker = UIDatePicker()

            picker.datePickerMode = .time

            picker.timeZone = NSTimeZone.local

            picker .frame = CGRect(x: 10,y : 50, width: self.view.frame.width, height: 200)



            self.view.addSubview(picker)

            

            

            title = "New timer"

                    

                    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Dismiss",

                                                                        style: .plain,

                                                                        target: self,

                                                                        action: #selector(dismissSelf))              

                    returnButton.setTitle("Push Another controller", for: .normal)

                    view.addSubview(returnButton)

                    returnButton .backgroundColor = .white

                    returnButton.setTitleColor(.black, for: .normal)

                    returnButton.frame = CGRect(x: 100, y: 100, width: 200, height: 52)

                    returnButton.addTarget(self, action: #selector(didTapreturmButton), for: .touchUpInside)

        }

        @objc private func didTapButtonValidate( sender: UIButton) {

            if let text = field.text, !text.isEmpty {
// the mistake "Cannot find 'picker' in scope"  is displayed just below 
            let pickedDatetime = picker.time

                completionHandler?(text, pickedDatetime)

                navigationController?.popToRootViewController(animated: true)

            }

        }

        @objc private func didTapreturmButton(){

                let vc = SecondViewController()

                vc.view.backgroundColor = .white

                navigationController?.pushViewController(vc, animated: true)

            }

            @objc private func dismissSelf() {

                dismiss(animated: true, completion: nil)
            }



        

        func textFieldShouldReturn(_ textField: UITextField) -> Bool {

            textField.resignFirstResponder()

            return true

        }

    }```
When I add the function over the  override func like that 

```Swift
        private let picker = UIDatePicker {

        picker.datePickerMode = .time
                        return picker
        }()

        ```
**** I get the error message "No exact matches in call to initializer " which I don't understand either. However the other error message disappears afterwards. Does anyone know how to fix this problem?

Thanks in advance****
Answered by OOPer in 699429022

In your code, picker is a local variable inside viewDidLoad(). So, the scope of your picker is only inside viewDidLoad().

You may need to declare an instance variable picker, and assign your picker to it in viewDidLoad().

        self.picker = picker
Accepted Answer

In your code, picker is a local variable inside viewDidLoad(). So, the scope of your picker is only inside viewDidLoad().

You may need to declare an instance variable picker, and assign your picker to it in viewDidLoad().

        self.picker = picker

@CarstenAwzerzewt as OOPer explained, your picker var does not exist anymore once viewDidLoad has completed.

What is probably confusing you is that the view has been added to the UI ; but you lost the reference to it, unless you do as OOPer told you.

Note:

function "picker "

picker is not a function, it is an instance of the UIPicker class (in fact here UIDatePicker).

function "picker " can't be found and I don't understand why
 
 
Q