0

I have the need to convert a string to Int so I can use an if > statement so I can enable and disable buttons. As you can see from the commented lines in the updateStopwatch I've tried a number of ways to convert but without any success

My Class looks like

class Stopwatch {

    var startTime:Date?

    func startTimer() {
        startTime = Date();
    }

    func elapsedTimeSinceStart() -> String {
        var elapsed = 0.0;
        if let elapsedTime = startTime {
            if firstHalfTime {
                elapsed = elapsedTime.timeIntervalSinceNow
            } else {
               elapsed = elapsedTime.timeIntervalSinceNow - 45*60
            }
        }
        elapsed = -elapsed
        let minutes = Int(floor((elapsed / 60)));
        let seconds = Int(floor((elapsed.truncatingRemainder(dividingBy: 60))));
//        print(elapsed)
        let timeString = String(format: "%02d:%02d", minutes, seconds)
//        print(timeString)
        return timeString
    }
}

My update timer function

func updateStopwatch() {

        let stopWatchString = stopWatch.elapsedTimeSinceStart()
        stopwatchLabel.text = stopWatchString

//        let minutesString:Int = Int(stopWatchString)!
//        minutes = minutesString

          if minutes > 1 {
            endFirstHalf.isEnabled = true
            self.endFirstHalf.alpha = 1
        }
    } 

1 Answer 1

1

You cannot convert a string back to Int after inserting a colon (:).

I recommend to return the TimeInterval

func elapsedTimeSinceStart() -> TimeInterval {
    var elapsed = 0.0
    if let elapsedTime = startTime {
        if firstHalfTime {
            elapsed = elapsedTime.timeIntervalSinceNow
        } else {
            elapsed = elapsedTime.timeIntervalSinceNow - 45*60
        }
    }
    elapsed = -elapsed
    return elapsed
}

and do the math in updateStopwatch()

func updateStopwatch() {

    let elapsed = stopWatch.elapsedTimeSinceStart()
    let minutes = Int(floor(elapsed / 60)));
    let seconds = Int(floor((elapsed.truncatingRemainder(dividingBy: 60))));
    let stopWatchString = String(format: "%02d:%02d", minutes, seconds)

    stopwatchLabel.text = stopWatchString

    if minutes > 1 {
        endFirstHalf.isEnabled = true
        self.endFirstHalf.alpha = 1
    }
} 

This code does the same but uses a bit more contemporary API

let dateComponentsFormatter : DateComponentsFormatter = {
    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = [.minute, .second]
    return formatter
}()

func elapsedTimeSinceStart() -> DateComponents {
    var components = DateComponents(minute: 0, second:0)
    if let elapsedTime = startTime {
        components = Calendar.current.dateComponents([.minute, .second], from: elapsedTime, to: Date())
        if !firstHalfTime { components.minute! += 45 }
    }
    return components
}

func updateStopwatch() {

    let components = elapsedTimeSinceStart()
    stopwatchLabel.text = dateComponentsFormatter.string(from: components)

    if components.minute! > 1 {
      endFirstHalf.isEnabled = true
      self.endFirstHalf.alpha = 1
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Are you saying I cant use if minutes > 1, I've just added a print (minutes) statement and I get a response of 0
No, you can't convert "1:30" to Int, because there is a non-digit character in the string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.