Search code examples
iosswiftxcodeunrecognized-selector

unrecognized selector sent to instance 0x7f90f1f04180


I'm a newbie to swift. I'm using xcode version 8 beta 4. I'm having an error whenever I scroll through my Date Picker. I have read a very similar problem, but the answer there is not fixing my problem. Below is my code:

func textFieldDidBeginEditing(_ textField: UITextField) {
    let datePicker = UIDatePicker()
    textField.inputView = datePicker
    datePicker.addTarget(self, action: Selector("datePickerChanged:"), for: .valueChanged)        
}

func datePickerChanged(sender: UIDatePicker){
    let formatter = DateFormatter()
    formatter.dateStyle = .long
    dateLog.text = formatter.string(from: sender.date)
}

when I click on my textfield the UIDatePicker is showing up just fine, but when I start to scroll through the dates, it gives me the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyApp.ViewController datePickerChanged:]: unrecognized selector sent to instance 0x7f90f1f04180'


Solution

  • Selector(String) is deprecated. You should start using the new syntax #selector.

    Also, the colon at the end is not needed.

    So, your code should look like this:

    datePicker.addTarget(self, action: #selector(datePickerChanged), 
        for: .valueChanged)