-1

I've got a problem with 'Declaration is only valid at file scope' error I was not modifying any other files than mainstoryboard. Those are all places. (whiteout one 'If != nil') where I was using TextFields. I've been trying nearly everything. Thanks for any help

Code:

  class ViewController: UIViewController {

    //inputs
    @IBOutlet weak var creditValueInput: UITextField! //wartość kredytu, input 
    @IBOutlet weak var procentageInput: UITextField! //oprocentowanie, input
    @IBOutlet weak var yearsInput: UITextField!  //ile lat/miesiecy, input

    @IBOutlet weak var creditOverallOutput: UILabel!
    @IBOutlet weak var creditCalculationOutput: UILabel!



    override func viewDidLoad() {
        super.viewDidLoad()


        self.creditValueInput.delegate = (self as! UITextFieldDelegate)
        self.procentageInput.delegate = (self as! UITextFieldDelegate)
        self.yearsInput.delegate = (self as! UITextFieldDelegate)
        // Do any additional setup after loading the view.
    }

  ......

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      creditValueInput.resignFirstResponder()
      procentageInput.resignFirstResponder()
      yearsInput.resignFirstResponder()

  .....


        extension ViewController: UITextFieldDelegate { !Declaration is only valid at file scope!

            func textFieldShouldReturn(_ textField: UITextField) -> Bool {
                textField.resignFirstResponder()
                return true
            }
        }

    }

Thanks for any help

Saq
  • 1
  • You can’t have an extension inside a function or a class, it needs to outside of any class/struct. So move it after the last } of your ViewController class – Joakim Danielson May 19 '19 at 10:54

1 Answers1

0

It seems like you are writing your extension inside your class.
While extensions are valid at file scope, so you should cut your extension block and paste it outside your class.

Assuming the braces of touchesBegan func are closed correctly.

Reem
  • 206
  • 2
  • 7