-2

My code is:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBOutlet weak var statePicker: UIPickerView!
    @IBOutlet weak var statePickerBTN: UIButton!

    let states = ["Alaska,Arkansas, Alabama, California, Maine, New York"]

    override func viewDidLoad() {
        super.viewDidLoad()
        statePicker.dataSource = self
        statePicker.delegate = self
    }

    @IBAction func statePickerButton(_ sender: Any) {
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return states.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return states[row]
    }
}

It says that it was "invalid redeclaration of 'viewDidLoad()'

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Tom Weston
  • 11
  • 1
  • Both redeclaration and recompile can cause the issue. Refer to [Invalid redeclaration of UITableViewDataSource method?](https://stackoverflow.com/q/27474798/6521116) and [Invalid redeclaration of ****](https://stackoverflow.com/q/50148125/6521116) – LF00 May 03 '18 at 07:25

2 Answers2

0

You wrote the function

 override func viewdidLoad() {
      super.viewdidLoad()
 }

twice in your code. Remove it. It will get resolved

KrishnaCA
  • 5,615
  • 1
  • 21
  • 31
0

I think you are new to Swift and iOS..Your error clearly shows that ""invalid redeclaration of 'viewDidLoad()'" you have declared the method viewDidLoad twice. Remove one. we cannot have multiple method with same name and arguments inside a class.

override func viewDidLoad() {
        super.viewDidLoad()
        statePicker.dataSource = self
        statePicker.delegate = self
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
Nilesh Jha
  • 1,626
  • 19
  • 35