title: Updating a function that get the exact time - Swift
body: <p>I have a function that gets the exact time. This is the code:</p>
  
  <pre><code>func getTime() {
      var date = NSDate()
      var outputFormat = NSDateFormatter()
      outputFormat.locale = NSLocale(localeIdentifier:"en_US")
      outputFormat.dateFormat = "HH:mm:ss"
      println(outputFormat.stringFromDate(date))
  }
  
  
  getTime()
  </code></pre>
  
  <p>It gets the exact time but does not update every second. I have a function that gets the exact time when the view loads, but it counts up just when the system can count up and the system can not keep up with the exact time, and it lags a couple seconds behind. </p>
  
  <p><strong>What function will make this update every second in real time</strong> </p>
  
  <p><strong>Code for getting the time</strong></p>
  
  <pre><code>var time = [String]()
  var startTime = NSTimeInterval()
  
  //This is the viewDidLoad Function
  override func viewDidLoad() {
      super.viewDidLoad()
      handler(theDatePicker)
      var timer = NSTimer()
      if !timer.valid {
          let selector : Selector = "countTime"
          timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target:self, selector: selector, userInfo: nil, repeats: true)
          startTime = NSDate.timeIntervalSinceReferenceDate()
      }
      let timeNow = timeNowString() as String
      for item in timeNow {
          time = timeNow.componentsSeparatedByString(":")
      }
  
      theDatePicker.addTarget(self, action: Selector("handler:"), forControlEvents: UIControlEvents.ValueChanged)
  
      func getTime() {
          var date = NSDate()
          var outputFormat = NSDateFormatter()
          outputFormat.locale = NSLocale(localeIdentifier:"en_US")
          outputFormat.dateFormat = "HH:mm:ss"
          timeLabel.text = (outputFormat.stringFromDate(date))
      }
  
  
  
  
  
  
  }
  
  
  func countTime() {
      var currentTime = NSDate.timeIntervalSinceReferenceDate()
      var elapsedTime: NSTimeInterval = currentTime - startTime
      var adjustedTime = Int(elapsedTime) + 3600*time[0].toInt()! + 60*time[1].toInt()! + time[0].toInt()!
  
      var hours = Int(Double(adjustedTime)/3600.0)
      let minutes = Int(Double(adjustedTime - hours*3600)/60.0)
      let seconds = adjustedTime - hours*3600 - minutes*60
  
      let startHours  = hours &gt; 9 ? String(hours):"0" + String(hours)
      let startMinutes  = minutes &gt; 9 ? String(minutes):"0" + String(minutes)
      let startSeconds  = seconds &gt; 9 ? String(seconds):"0" + String(seconds)
  
      //timeLabel.text = "\(startHours):\(startMinutes):\(startSeconds)"
  }
  
  
  func timeNowString() -&gt; NSString {
      let date = NSDate()
      var outputFormat = NSDateFormatter()
      outputFormat.locale = NSLocale(localeIdentifier:"en_US")
      outputFormat.dateFormat = "HH:mm:ss"
      let timeString = outputFormat.stringFromDate(date)
      return timeString;
  }
  </code></pre>
  
  <p><strong>viewDidAppear Function</strong></p>
  
  <pre><code>override func viewDidAppear(animated: Bool) {
          func getTime() {
              var date = NSDate()
              var outputFormat = NSDateFormatter()
              outputFormat.locale = NSLocale(localeIdentifier:"en_US")
              outputFormat.dateFormat = "HH:mm:ss"
              timeLabel.text = (outputFormat.stringFromDate(date))
          }
  
          let timer = NSTimer(timeInterval: 1, target: self, selector: "getTime", userInfo: nil, repeats: true)
  
          timeLabel.text = "\(timer)"
      }
  </code></pre>