Reputation:
I want the user the to enter a number in the textfield and when the timer hits that number the timer stops. I am trying it right now but its saying I can't use boolean operators to do this.
import UIKit
class ViewController: UIViewController {
@IBOutlet var playbutton: UIButton!
@IBOutlet var titlelabel: UILabel!
@IBOutlet var judo: UITextField!
var timer : Timer?
var counter = 0.0
var isRunning = false
override func viewDidLoad() {
super.viewDidLoad()
titlelabel.text = "\(counter)"
playbutton.isEnabled = true
}
@IBAction func btnplay(_ sender: UIButton) {
if timer == nil {
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(UpdateTime), userInfo: nil, repeats: true)
playbutton.isEnabled = false
}
}
@objc func UpdateTime(){
counter += 0.1
titlelabel.text = String(format: "%.1f", counter)
if counter >= (Int(judo.text!)) {
timer?.invalidate()
timer = nil
}
}
}
Upvotes: 0
Views: 47
Reputation: 19156
You can't use binary operator with operands of type Double
and Int
. counter
is Double
so the right operand must be Double
let max = Double(judo.text ?? "") ?? 0.0
if counter >= max {
timer?.invalidate()
timer = nil
}
Upvotes: 1