New to the quick: why is the timer not updating to UILabel?

I am trying to create a custom clock using swift. I originally wrote this in python but figured it would be a good opportunity to learn a new language but Im having a rough idea to it. I've read many great answers about using Timer () but nothing seems to work; it will update once and remain static. Here is my code:

import UIKit
import Foundation


class ViewController: UIViewController {

@IBOutlet weak var timeLabel: UILabel!
let clock = MarsTime()
var timer: Timer?

override func viewDidLoad() {
    super.viewDidLoad()

    timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] timer in
        self?.updateTimeLabel()
            }



}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    updateTimeLabel()


}

@objc func updateTimeLabel() {

    let millis = clock.currentTimeMillis()
    let jdUT = clock.julianDateUT(millis: millis)
    let jdTT = clock.julianDateTT(julianDateUT: jdUT)
    let mct = clock.marsCoordinatedTime(julianDateTT: jdTT)
    var mctClockTime = clock.clockTime(mct: mct)
    let hour = mctClockTime[0]
    let min = mctClockTime[1]
    let sec = mctClockTime[2]
    timeLabel.text = "\(hour):\(min):\(sec)"
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

deinit {
    NotificationCenter.default.removeObserver(self)
}

}

      

Thanks for the help provided!

Update: I've also included the MarsTime code

class MarsTime {

//---------------------------------\\
// Configure Earth Time Functions  \\
//---------------------------------\\

let date = Date().timeIntervalSince1970



func currentTimeMillis() -> (Int) {// Convert time to milliseconds
    let currentTimeMillis = Int(date * 1000)
    return (currentTimeMillis)
}

func julianDateUT(millis: Int) -> (Double) { // Convert julian date universial time
    let julianDateUT = 2440587.5 + (Double(millis) / (8.64 * pow(10, 7)))
    return (julianDateUT)
}

func julianDateTT(julianDateUT: Double) -> (Double) {// Convert to julian date Terrestrial time
    let julianDateTT = julianDateUT + ((32.184 + 37.0) / (86400.0))
    return (julianDateTT)
}

func deltatJ2000(julianDateTT: Double) -> (Double) {// Calculate time offset from J2000 Epoch
    let deltatJ2000 = julianDateTT - 2451545.0
    return (deltatJ2000)
}
//---------------------------------\\
//Configure Martian Time Fucntions \\
//----------------------------------\\

func marsMeanAnomaly(deltatJ2000: Double) -> (Double) { // Calculate the mean anomaly of the martian orbit
    let maUncorrected = 19.3871 + 0.52402073*(deltatJ2000)
    let n360s = Int( maUncorrected / 360.0) * 360
    let marsMeanAnomaly = maUncorrected - Double(n360s)
    return marsMeanAnomaly
}

func angleFictionMeanSun(deltatJ2000: Double) -> (Double) {  // Calulate angle of diction mean sun
    let afmsUncorrected = 270.3871 + 0.524038496*(deltatJ2000)
    let n360s = Int(afmsUncorrected / 360.0) * 360
    let angleFictionMeanSun = afmsUncorrected - Double(n360s)
    return (angleFictionMeanSun)
}

func perturbers(deltatJ2000: Double) -> (Double) { // Calculate perturbers
    let pbs = 0.0071 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/2.2353) + 49.409)) +
        0.0057 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/2.7543) + 168.173)) +
        0.0039 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/1.1177) + 191.837)) +
        0.0037 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/15.7866) + 21.736)) +
        0.0021 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/2.1354) + 15.704)) +
        0.0020 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/2.4694) + 95.528)) +
        0.0018 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/32.8493) + 49.095))
    return (pbs)
}

func v_M(deltatJ2000: Double, pbs: Double, marsMeanAnomaly: Double) -> (Double) { // Determine the equation of center
    let A = (10.691 + (3*pow(10, -7))*deltatJ2000)
    let B = Int((10.691 + (3*pow(10, -7))*deltatJ2000) / 360)*360
    let leadingConstant = A + Double(B)
    let v_M = (leadingConstant)*sin(.pi / 180.0 * (marsMeanAnomaly)) +
        0.623*sin(.pi / 180.0 * (2*marsMeanAnomaly)) + 0.050*sin(.pi / 180.0 * (3*marsMeanAnomaly)) +
        0.005*sin(.pi / 180.0 * (4*marsMeanAnomaly)) + 0.0005*sin(.pi / 180.0 * (5*marsMeanAnomaly)) +
    pbs
    return (v_M)
}

func aerocentSolarLong(angleFictionMeanSun: Double, v_M: Double) -> (Double) {
    let l_s = (angleFictionMeanSun + v_M) - Double(Int((angleFictionMeanSun + v_M) / 360)*360)
    return (l_s)
}

func martianEquationOfTime(l_s: Double, v_M: Double) -> (Double) {
    let eot = 2.861*sin(.pi / 180.0 * (2*l_s)) - 0.071*sin(.pi / 180.0 * (4*l_s)) +
        0.002*sin(.pi / 180.0 * (6*l_s)) - v_M
    return (eot)
}



func marsCoordinatedTime(julianDateTT: Double) -> (Double) {
    let mct = (24.0 * (((julianDateTT - 2451549.5) / 1.0274912517) + 44769.0 - 0.0009626))
    let mctFinal = mct.truncatingRemainder(dividingBy: 24.0)
    return (mctFinal)
}

func localMeanSolarTime(mct: Double, deg: Double) -> (Double)  { // Solar time for any longtitude west of the prime meridian
    let lmst = mct - deg*(1.0 / 15.0)
    return (lmst)
}


func marsDistance(ma: Double) -> (Double) {
    let helioDistance = 1.5236*(1.00436 - 0.09309*cos(.pi / 180.0 * (ma))
        - 0.00436*cos(.pi / 180.0 * (2*ma)) - 0.00031*cos(.pi / 180.0 * (3*ma)))
    return (helioDistance)
}

//-------------------------\\
// Configure Clock Display \\
//-------------------------\\
func clockTime(mct: Double) -> Array<String> {


    let mctStr = String(mct)
    var strHours = mctStr.components(separatedBy: ".")
    var mctHours  = strHours[0]
    var strMin = String(60 * Double("." + strHours[1])!).components(separatedBy: ".")
    var mctMin = strMin[0]
    var strSec = String(60 * Double("." + strMin[1])!).components(separatedBy: ".")
    var mctSec = strSec[0]
    let sec = mctSec.characters.count
    let min =  mctMin.characters.count
    let hour = mctHours.characters.count
    if 1 <= sec && sec < 2 {
        mctSec = "0" + mctSec
    } else if mctSec.characters.count < 1 {
        mctSec = "00"
    }
    if 1 <= min && min < 2 {
        mctMin = "0" + mctMin
    } else if mctMin.characters.count < 1 {
        mctMin = "00"
    }
    if 1 <= hour && hour < 2 {
        mctHours = "0" + mctHours
    } else if hour < 1 {
        mctHours = "00"
    }

    let mctClockTime = [mctHours , mctMin ,  mctSec]
    return (mctClockTime)
}
}

      

+3


source to share


2 answers


Your timer is working correctly. Your label refreshes every second, but it always refreshes to the same value.

The problem is that you only compute date

once when you create an object MarsTime

.



One way to fix this is to make a date

computed property that will always return the current time every time it is read:

var date: TimeInterval { return Date().timeIntervalSince1970 }

      

+2


source


Try the following:



timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(ViewController.updateTimeLabel)), userInfo: nil, repeats: true)

      

-1


source







All Articles