简体   繁体   English

如何快速追踪时间?

[英]how to track time in swift?

I need to make an app to track the employee attendance in an office, i will get the time date and time from the server as a string after that I need to update Time label, and then convert it time Date class for the next process. 我需要制作一个应用程序来跟踪办公室的员工出勤情况,之后我需要从服务器以string获取时间日期和时间,之后我需要更新时间标签,然后将其转换为时间Date类以用于下一个流程。

在此处输入图片说明

class MainMenuVC: UIViewController {

    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var checkinStatusLabel: UILabel!

    var employee : Employee?
    var time : String?
    var date : String?

    override func viewDidLoad() {
        super.viewDidLoad()

        getDateTimeFromServer()

    }


func getDateTimeFromServer() {

// request to server to get date and time
// update the UI (date and time label)

}





}

but the problem is, the time data will always be the same, of course since I request only once to the server so the 'minute' will be static. 但是问题是,时间数据将始终是相同的,当然,由于我仅向服务器请求一次,因此“分钟”将是静态的。 it will be a problem for this attendance app. 这将是这个出勤应用程序的一个问题。

let say when the user open that menu on 15:23, but when they actually tap the checkin button 7 minutes later, so the data that will be sent back to server is 15:23 (when the viewDidLoad firstly activated) not 15:30. 假设用户在15:23时打开该菜单,但是当他们在7分钟后实际点击签入按钮时,则将发送回服务器的数据是15:23(首次激活viewDidLoad时)而不是15:30 。

so how do i tackle this problem so the time will always be updated and will always be the same as the time from server for my UI label and for my Date class ? 那么我该如何解决这个问题,以便时间总是更新,并且总是与服务器上UI标签和Date类的时间相同?

This is a simple programming problem. 这是一个简单的编程问题。 You either need to 你要么需要

  1. Request the time from the server each time you need a time. 每次需要时间时,请从服务器请求时间。

or 要么

  1. Fetch the time from the server once, convert it to a Date , get the current date from your device, calculate the difference, and use that difference as an offset. 从服务器获取一次时间,将其转换为Date ,从设备中获取当前日期,计算差值,并将该差值用作偏移量。 The next time the user taps the checkin button, get the current Date from the device, add your offset, and use that as the current Date . 下次用户点击签到按钮时,从设备获取当前Date ,添加您的偏移量,并将其用作当前Date

I leave it to you to figure out the details of how to do that. 我留给您弄清楚如何做的细节。 Take a look at the Date class reference in Xcode. 看一下Xcode中的Date类引用。 There are methods that let you convert between Dates and numeric values, and apply numeric offsets to Dates . 有一些方法可让您在Dates和数值之间进行转换,并将数字偏移量应用于Dates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM