未获得手机当前时间的日期Swift 4

时间:2018-04-29 21:39:07

标签: swift nsdate

我有这个功能可以计算手机当前分钟的时间。我想在这个特定的时间后让计时器关闭。调用此函数的方式是通过用户在应用程序中翻转的开关,时间应该自行重置。好吧,我的NSDate对象遇到问题,以前在切换开关时获取旧值。有没有办法将NSDate对象重置为零?

这是我计算手机当前时间的代码。

func GetInitialTime(){
    finalTime = 0
    firstTimeCounter = 0
    timeInSeconds = 0
    let calendar = NSCalendar.current
    var minutes = calendar.component(.minute, from: date)
    var timeDifference = Int()

    if(minutes == 00 || minutes == 30) {
        print("The minute hand is at zero or thirty.")
    }
    else {
        print("The minute hand is NOT ar zero or thirty")
        print("The minute hand ia at:")
        if minutes < 30 {
            while (!(minutes == 30)) {
                minutes += 1
                timeDifference += 1
            }
            print("Therefore we make the minute hand at zero or thiry: ", minutes)
            print("The time difference we add to the minute is: ", timeDifference)
        }
        else {
            var i = minutes
            while i < 60 {
                i += 1
                minutes += 1
                timeDifference += 1
            }
            print("Therefore we make the minute hand at zero or thirty: ", minutes)
            print("The Time difference we add to the minute is: ", timeDifference)
        }

    }
    finalTime = Double(timeDifference * 60)
    print("The time difference in seconds is:", finalTime)
}

我在这里声明了Date()对象

let center = UNUserNotificationCenter.current()
let button = UIButton(type: UIButtonType.custom)
let date = Date()
var timeInSeconds = Int()
var finalTime = Double()
var halfHour = Double(1800)
var firstTimeCounter = Int()
var firstTimer = Timer()
var repeatingTimer = Timer()
var backgroundTask = BackgroundTask()
let dispatchGroup = DispatchGroup()
var urlWeb = "http://morrowrenewablesflowdata.com/iOSConnections/Notifications.php"
var downtimes = [String]()
var flows = [String]()

1 个答案:

答案 0 :(得分:1)

您的代码使用的是一个常量变量date。您没有显示它所设置的上下文,但我假设它是您类的实例变量。它是一个常量的事实意味着它永远不会在它声明的范围内发生变化。这几乎肯定是你的问题。