Swift中按钮按下的时间差异

3

现在我正在开发一个时间表应用程序,允许用户打卡记录工作时间。但是我遇到了一些问题,不知道如何实现这个功能。

我的模型中有:

struct TimeLog {

var punchInTime: CFAbsoluteTime
var punchOutTime: CFAbsoluteTime
var timeWorked: Double


init (pInTime: CFAbsoluteTime, pOutTime: CFAbsoluteTime) {
    self.punchInTime = pInTime
    self.punchOutTime = pOutTime
    self.timeWorked = pOutTime - pInTime
}

}

在我的视图控制器中:

class ViewController: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

@IBAction func punchInButtonPressed(sender: AnyObject) {

// not sure what actions are needed here to start the "aTimeLog" variable

}
@IBAction func punchOutButtonPressed(sender: AnyObject) {

// not sure what actions are needed here to complete the "aTimeLog" variable

}

我正在尝试完成这个变量:
var aTimeLog = TimeLog(pInTime: //get the punch in time here, pOutTime: //get the punch out time here)

一旦变量"aTimeLog"完成(即按下punchOutButton),我希望显示所有"timeWorked"变量的日志记录。

提前道歉,您可能能够看出我正在学习编程和Swift。

4个回答

1
作为可能的解决方案之一,您可以按照以下方式更改您的init方法:
initWithStartTime (pInTime: CFAbsoluteTime) 
{
    self.punchInTime = pInTime
}

然后添加一个类似的方法:
func workedTimeWithEndTime(pOutTime: CFAbsoluteTime) -> CFAbsoluteTime
{
    return pOutTime - self.punchInTime
}

最后,您可能需要将“var timeLog:TimeLog!”添加到您的视图控制器中,并在punchInButtonPressed中使用开始时间进行初始化。

1

个人而言,我会在NSUserDefaults中保留一个变量——存储打卡时间。然后,当点击打卡按钮时,它从NSUserDefaults获取dateTime变量,进行必要的计算并返回结果。这样即使用户关闭应用程序(打卡时间仍会被记住),也能满足需求。

这将创建一个NSUserDefaults对象。

var punchInTime : NSUserDefaults = NSUserDefaults.standardUserDefaults()

    @IBAction func punchInButtonPressed(sender: AnyObject) {

保存当前时间。
        punchInTime.setObject(NSDate(), forKey: "punchInTime")
        punchInTime.synchronize()
    }
    @IBAction func punchOutButtonPressed(sender: AnyObject) {

获取保存的时间
        punchInTime.objectForKey("punchInTime") as NSDate

创建一个变量来保存当前时间,以便您可以将其与另一个时间进行比较。
        var currentTime : NSDate = NSDate()
        currentTime.timeIntervalSinceDate(punchInTime) // returns seconds

现在,您可以将开始时间、结束时间和总工作时间保存在一个字典中,例如以日期为键。
    }

然后,当您想要显示所有工作日时,只需循环遍历字典 :)
...希望我理解得正确。

这是有意义的,因为我希望打卡时间即使应用程序关闭也能被记住。我输入了代码,但我认为我对它的工作原理感到困惑,并且在“sliderTime”上出现了未解决的标识符(不确定它来自哪里),并且还说NSUserDefaults无法转换为NSDate。除此之外,我还不知道如何将其存储在字典中。(我希望允许用户在表视图中查看它们)。...对不起我的无知,我想尽可能学习。 :) - Leighton
抱歉,那是我的错误...已经更正了。上面的代码不能直接使用,你需要修复并进行调试。我已经为你做了一个简单的示例,网址是http://pastebin.com/Sv4pSyrA,但是它只能解决你的一些问题。学习的唯一方法就是自己去摸索 :) 坚持下去! - Byron Coetsee
也许我漏掉了什么,但我不明白如何从timeIntervalSinceDate函数中提取日期。 - Leighton
如果您查看我的代码中的注释,它将返回一个整数,表示自提供日期以来的秒数。它没有要从中提取的日期。名称“'timeInterval'SinceDate”也应该能说明这一点... - Byron Coetsee

0

试试这个:

// run this on first action/tap
let start = Date()

// then, run this on the second time/interaction, to get the negative elapsed time
// for example: for 0.8 seconds this will return -0.8694559335708618
print("Elapsed time: \(start.timeIntervalSinceNow) seconds")

0
你应该给你的 ViewController 类一个 TimeLog 类的引用(为什么选择结构体?也可以,但只是好奇)。
var timeLog: TimeLog?

其次,您希望每次输入时都使用新的初始化。

@IBAction func punchInButtonPressed(sender: AnyObject) {
    let now = CFAbsoluteTimeGetCurrent()
    self.timeLog = TimeLog(pInTime: now)
}

所以你只需使用pInTime进行初始化,其余时间设置为0 / null。接下来,当你打卡下班时,添加pOutTime,就可以计算出工作时间。

@IBAction func punchOutButtonPressed(sender: AnyObject) {
    if (self.timeLog == nil) {
        return
    }

    self.timeLog!.punchOut(CFAbsoluteTimeGetCurrent());
    println(self.timeLog!.timeWorked)
}

由于我们不使用您创建的构造函数并且需要一个新函数,因此我们需要更改您的类(除非您知道为什么要使用结构体,请使用类)。

class TimeLog {

var punchInTime: CFAbsoluteTime
var punchOutTime: CFAbsoluteTime
var timeWorked: Double


init (pInTime: CFAbsoluteTime) {
    self.punchInTime = pInTime
    self.punchOutTime = 0
    self.timeWorked = 0
}

func punchOut(pOutTime: CFAbsoluteTime) {
self.punchOutTime = pOutTime
self.timeWorked = self.punchOutTime - self.punchInTime
}

}

当我输入:self.timeLog = TimeLog(pInTime: now)我在punchInButtonPressed Action中得到了一个缺少参数“pOutTime”的错误提示。 - Leighton
不确定为什么,但我仍然遇到了很多错误。 - Leighton
我做了一些小的更改,因为我没有再次将代码放入Xcode中。 - Lucas van Dongen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接