UIPickerView selected time to run code

I have added in a UIPickerView and currently have it store the selected time as a string. I want the app to carry out a simple line of code when the time that was selected on the pickerview is the time in the real world. Here is the code that I have added.

For the Clock, used to find the real world time:

  • Comparing NSDates without time component
  • NSDateFormatter Question
  • Determine if current local time is between two times (ignoring the date portion)
  • Get Unix Epoch Time in Swift
  • get time between two times of the day
  • Function to return open, closed or closing from Hours of day
  • let clockString: String = formatADate()
    func formatADate() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .short
        dateFormatter.dateFormat = "hh:mm:ss a"
        let date = NSDate()
        let output = dateFormatter.string(from: date as Date)
        return output

    Here is the code for the UIPickerView:

    @IBOutlet var dateTimeDisplay: UILabel!
    @IBOutlet var datePicker: UIDatePicker!
    @IBAction func datePickerchanged(_ sender: Any) {
    func setDateAndTime() {
        let formatter = DateFormatter()
        formatter.dateFormat = "hh:mm:ss a"
        _ = formatter.string(from: datePicker.date)
        str = dateFormatter.string(from: (datePicker?.date)!)
        dateTimeDisplay.text = str

    And here is what I want to happen when the selected time and the real world time match up:

    takePhoto = true

    Solutions Collect From Internet About “UIPickerView selected time to run code”

    When the pick date the start one timer function

    call the function in picker

    var timercount = Timer()
     timercount = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(Check), userInfo: nil, repeats: true)
         Func Check()
         let nowdate = NSDate()//two date declare global Var
         let date2 = datePicker?.date //chek the Time How Much Time Remain
         let elapsed = date2?.timeIntervalSince(nowdate as Date)
         if Int(elapsed!) == 0
                takePhoto = true