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:

  • UILabel with numberOfLines and lineBreakMode
  • Generic protocol for basic types
  • Size of the NSInputStream buffer
  • Images lose quality after saving as GIF
  • Custom UIPageControl with dot images
  • “Use of undeclared type” in Swift, even though type is internal, and exists in same module
  • 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