【问题标题】:Subtracting from Int从 Int 中减去
【发布时间】:2015-12-07 07:40:02
【问题描述】:

我有一个 Int 我想从中减去,但我很困惑为什么代码会运行,但 Int 不做减法,有什么我遗漏的吗?

func calculateTime(theEmployee : Employee, thePunch : TimeClockPunchObj) {

    if thePunch.valueForKey("punchOutIn") as! String == "out" {

        let theLastPunchDate = self.lastPunch?.valueForKey("timePunched") as! NSDate
        var theMinutes = NSDate().minutesFrom(theLastPunchDate)
        print(lunchResult!)
        if (lunchResult!) {
            theMinutes - 20
        }
        print(theMinutes)
        let hours = String(format: "%.2f", (Double(theMinutes) / 60.00))
        print(hours)

        let timeCalc = TimePunchCalcObject()
        timeCalc.employee = theEmployee
        timeCalc.timePunchedIn = self.lastPunch?.valueForKey("timePunched") as! NSDate
        timeCalc.timePunchedOut = thePunch.valueForKey("timePunched") as! NSDate
        timeCalc.totalTime = Double(hours)!
        if Double(hours)! != 0.00 {
            timeCalc.saveInBackground()
        }
    }
}

【问题讨论】:

  • 请阅读 valueForKey 和 objectForKey 之间的区别。你真的,真的不想在这里使用 valueForKey。

标签: ios swift int subtraction


【解决方案1】:

这样做:

theMinutes = theMinutes - 20

或者(就像@vadian 提醒的那样):

theMinutes -= 20

【讨论】:

    【解决方案2】:

    或者只是添加等号

    theMinutes -= 20
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      相关资源
      最近更新 更多