In my application I need to show the date chosen with a DatePicker in a Label. I have the following code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var pickerDat: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
pickerDat.addTarget(self, action: Selector("diaHora:"), for: UIControlEvents.valueChanged)
pickerDat.datePickerMode = .date
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func diaHora(pickerDat: UIDatePicker){
let formatoFechaHora = DateFormatter ()
formatoFechaHora.dateStyle = DateFormatter.Style.short
let dat = formatoFechaHora.string(from: pickerDat.date)
label.text = dat
}
}
When choosing the date in the application, the following error appears:
I think the error is in the passage of the action parameter in the method of adding the target, it may be that the new version of Swift does not support that syntax.
I have found the solution, the new del syntax
Selector()
is#selector()
that a string with a colon as the last character is not passed as a parameter, but directly to the function. So the code goes from:a: