标签intValue不能显示大于(>)的值

时间:2018-10-30 17:11:02

标签: swift

我是Swift的新手,代码构建的很好,但是大于(>)的代码无法正常工作。我正在尝试以“ totalCoal”标签中的某个数字进行生产,但从未尝试过“ coalPileHolding” Second标签。我知道这段代码可以做的更好,但是我想先掌握基本的知识。我也知道timeDiffernt“>”也不起作用,所以我不知何故。谢谢您的帮助

 @IBOutlet weak var coalPileHoldingLabel: UILabel!
func loadBigCoalPile () {
    var coalPileHolding = Int ()
    if UserDefaults.standard.object(forKey: "coalPileResearch") == nil {
        coalPileHolding = 0 } else {
        coalPileHolding = UserDefaults.standard.object(forKey: "coalPileResearch") as! Int}
    if coalPileHolding == 1 {
        let coalPileHolding = 200
        coalPileHoldingLabel.text = String(coalPileHolding) }
    if coalPileHolding == 2 {
        let coalPileHolding = 300
        coalPileHoldingLabel.text = String(coalPileHolding) }
    if coalPileHolding == 3 {
        let coalPileHolding = 400
        coalPileHoldingLabel.text = String(coalPileHolding) }

@objc func buttonIsInAction(){

}
@IBOutlet weak var coalRunButton: UIButton!

@IBAction func coalRunButton(_ sender: Any) {

    func getMillisecondsNow() -> Int64{
        let currentDate = Date()
        return getMillisecondsFromDate(date: currentDate)
    }
    func getMillisecondsFromDate(date: Date) -> Int64{
        var d : Int64 = 0
        let interval = date.timeIntervalSince1970
        d = Int64(interval * 1000)
        return d
    }
    func getTimeDifferenceFromNowInMilliseconds(time: Int64) -> Int64{
        let now = getMillisecondsNow()
        let diff: Int64 = now - time
        return diff
    }
    var terminationTime = Int64()
    if UserDefaults.standard.object(forKey: "latestTerminationDate") == nil {
        terminationTime = getMillisecondsNow()
        UserDefaults.standard.set(terminationTime, forKey:"latestTerminationDate")
        }
    else {

     terminationTime = UserDefaults.standard.object(forKey: "latestTerminationDate") as! Int64 }

   let timeDiff = getTimeDifferenceFromNowInMilliseconds(time: terminationTime)
    let timeDiffernt = Int(timeDiff)
    let now = getMillisecondsNow()
    UserDefaults.standard.set (now, forKey: "latestTerminationDate")
    if  timeDiffernt > 86400000 { _ = 86400000}

    var methodOfCut = Int ()
    var machineryButton = Int ()
    var qualityOfWorkers = Int ()
    if UserDefaults.standard.object(forKey: "methodOfCut") == nil {
        methodOfCut = 0 } else {
        methodOfCut = UserDefaults.standard.object(forKey: "methodOfCut") as! Int}
    if UserDefaults.standard.object(forKey: "machineryButton") == nil {
        machineryButton = 0 } else {
        machineryButton = UserDefaults.standard.object(forKey: "machineryButton") as! Int}
    if UserDefaults.standard.object(forKey: "qualityOfWorkers") == nil {
        qualityOfWorkers = 0 } else {
        qualityOfWorkers = UserDefaults.standard.object(forKey: "qualityOfWorkers") as! Int}

    let coalMayham = (machineryButton) + (qualityOfWorkers) + (methodOfCut)
    let (dailyCoalAccumulate) = ((timeDiffernt) * (coalMayham) + 1) / 10000

    var coalPileHolding2 = 0
    if let coalPile = Int(coalPileLabel.text!) {
    let totalCoal = (dailyCoalAccumulate) + coalPile
         coalPileHolding2 = Int(coalPileHoldingLabel.text!) ?? 0
    if totalCoal > coalPileHolding2 { coalPileHolding2 = totalCoal }

        coalPileLabel.text = String(totalCoal)
            UserDefaults.standard.set(totalCoal, forKey:"totalCoal")}
        callOutLabel.text = String(dailyCoalAccumulate)}}

1 个答案:

答案 0 :(得分:0)

混合使用数字类型(Int32,Float,Int)非常令人困惑。通常,您要使用Int或Double。所有其他变体仅应在绝对必要时使用,例如,如果API需要其他类型。因此,假设sizeof(Bar)为Int并将其他所有内容也都切换为Int:

dailyCoalAccumulate

这里let coalPileHolding = 0 if let coalPile = Int(coalPileLabel.text!) { let totalCoal = dailyCoalAccumulate + coalPile let coalPileHolding = Int((coalPileHoldingLabel.text as! NSString).intValue) if totalCoal > coalPileHolding { let coalPileHolding = totalCoal } coalPileLabel.text = String(totalCoal) UserDefaults.standard.set(totalCoal, forKey:"totalCoal") } callOutLabel.text = String(dailyCoalAccumulate) 的{​​{1}} API返回intValue,但我立即将其转换为常规Int。但是,当然,有一种更好的方法可以执行此操作,而不必桥接到Objective-C NSString。如果字符串不包含数字Int32,则仅返回零。当我们使用Int初始化程序转换字符串,然后将NSString的值替换为零时,我们可以产生相同的行为:intValue

然后,我们有三个名为nil的变量。由于它们是在不同的作用域中定义的,因此它们可以共享相同的名称,但是仍然是不同的变量。我的猜测是您实际上想更新Int(coalPileHoldingLabel.text!) ?? 0变量。否则,内部if的赋值是没有意义的-编译器甚至会发出警告。

因此,让我们将coalPileHolding更改为coalPileHolding并更新其值。

coalPileHolding