Timer.scheduledTimer not executing selector function - swift

I created a app to check whether an integer is prime. It works fine, however I wanted to add a timer to time how long it takes to calculate some of the bigger primes. I created a timer variable but the Timer.scheduledTimer doesn't execute the addSecond function. What am I doing wrong? Thanks
class ViewController: UIViewController {
var number = 0.0
var timer = Timer()
var time = 0
func addSecond() {
time += 1
}
#IBOutlet weak var txtNumber: UITextField!
#IBOutlet weak var lblOutput: UILabel!
#IBAction func btnCalculate(_ sender: Any) {
print("calculating")
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.addSecond), userInfo: nil, repeats: true)
var i = 1
var divs = 0
number = Double(txtNumber.text!)!
while i <= Int(number)/2 {
if number.truncatingRemainder(dividingBy: Double(i)) == 0 {
divs += 1
}
i += 1
}
if divs > 1 {
lblOutput.isHidden = false
lblOutput.text = String(number) + " is not prime"
timer.invalidate()
} else {
lblOutput.isHidden = false
lblOutput.text = String(number) + " is prime. It took " + String(time) + " seconds to calculate it"
timer.invalidate()
}
}

Related

Error in Swift 5 with Xcode when creating a timer out of a label

I'm using Swift 5 in Xcode to code an app. On one view controller, I am creating a timer, which counts from 20 minutes down to 0. I have what I think is a successful code, but it throws back one error. In the line
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(SkippingViewController.updateTimer), userInfo: nil, repeats: true)
it gives an error saying Type 'SkippingViewController' has no member 'updateTimer' (SkippingViewController is the name of the view controller for the page of the app my timer is on)
How can I resolve this issue?
import UIKit
class SkippingViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var startWorkoutButton: UIButton!
#IBOutlet weak var pauseWorkoutButton: UIButton!
var timer = Timer()
var counter = 20.00
var isRunning = false
override func viewDidLoad() {
super.viewDidLoad()
timeLabel.text = "\(counter)"
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
// Do any additional setup after loading the view.
}
#IBAction func startWorkoutButtonDidTouch(_ sender: Any) {
if !isRunning {
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(SkippingViewController.updateTimer), userInfo: nil, repeats: true)
startWorkoutButton.isEnabled = false
pauseWorkoutButton.isEnabled = true
isRunning = true
}
}
#IBAction func pauseWorkoutButtonDidTouch(_ sender: Any) {
func updateTimer() {
counter -= 0.01
timeLabel.text = String(format: "%.01f", counter)
}
}
}
Your problem is, that there is no method called 'updateTimer' in SkippingViewController.swift. You falsely put the method inside of the method 'pauseWorkoutButtonDidTouch'. In order to resolve the error insert the following code into SkippingViewController.swift:
#objc func updateTimer() {
counter -= 0.01
timeLabel.text = String(format: "%.01f", counter)
}

updating label variables from a running timer

I'm trying to create a counter that does simple math when the timer reaches zero. I have my timer working somewhat correctly, and the labels displaying the original variables, but they are not updating as the timer hits "zero". Where am I going wrong in my code?
class ViewController: UIViewController {
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var goldCounter: UILabel!
#IBOutlet weak var turnCounter: UILabel!
var seconds = 15
var timer = Timer()
var gold = 1000
var turns = 1
func updatelabels () {
goldCounter.text = String(gold)
turnCounter.text = String(turns) }
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer),userInfo: nil, repeats: true)
}
#objc func updateTimer() {
seconds -= 1
timerLabel.text = String(seconds)
if seconds == 0 {
seconds = 15}
}
func increaseGold () {
if seconds == 1 {
gold = gold + 1000
turns = turns + 1
}}
override func viewDidLoad() {
super.viewDidLoad()
self.runTimer()
goldCounter.text = String(gold)
turnCounter.text = String(turns)
// Do any additional setup after loading the view, typically from a nib.
func increaseGold () {
if seconds == 1 {
gold = gold + 1000
turns = turns + 1
}}
func updatelabels () {
goldCounter.text = String(gold)
turnCounter.text = String(turns) }
}
}
Your main issue is that you aren't calling updateLabels to update your labels.
I would suggest using property observers (didSet) to set your labels as the values change instead of relying on a separate function to change them.
Also, you need to call increaseGold in your timer handler:
class ViewController: UIViewController {
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var goldCounter: UILabel!
#IBOutlet weak var turnCounter: UILabel!
var seconds = 15 {
didSet {
timerLabel.text = String(seconds)
}
}
var timer = Timer()
var gold = 1000 {
didSet {
goldCounter.text = String(gold)
}
}
var turns = 1 {
didSet {
turnCounter.text = String(turns)
}
}
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer),userInfo: nil, repeats: true)
}
#objc func updateTimer() {
seconds -= 1
if seconds == 0 {
seconds = 15
}
increaseGold()
}
func increaseGold () {
if seconds == 1 {
gold = gold + 1000
turns = turns + 1
}
}
override func viewDidLoad() {
super.viewDidLoad()
seconds = 15
gold = 1000
turns = 1
self.runTimer()
}
}
I think when you added let, your problem is gone. Delete var timer = Timer(). It is not neccessary
func runTimer() {
let timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer),userInfo: nil, repeats: true)
}

Swift Progress View with NSTimer

I have a Progress view bar that I would like to use to indicate time. This is my first project in Swift, and I am unsure how to go about this. So any help/ advise would be appreciated ...
(Using Xcode 7.2 and Swift 2.0)
Below Is my view controller. When 'btnPlaySession' is triggered, the content on the view controller is changed every 20 seconds. While the timer is counting to 20, id like to indicate this with the progress bar (so the progress bar resets, each time the content changes).
class CreatedSessionViewController: UIViewController {
var createdSession: [YogaPose]!
var poseDuration: Double = 20.00
var timer = NSTimer!()
var currentPoseIndex = 1
//Outlets:
#IBOutlet var poseProgressView: UIProgressView!
#IBOutlet var lblPoseCount: UILabel!
#IBOutlet var lblPoseName: UILabel!
#IBOutlet var imgPose: UIImageView!
#IBOutlet var tvDescription: UITextView!
// Do any additional setup after loading the view:
override func viewDidLoad() {
super.viewDidLoad()
displayFirstPoseInArray()
}
func displayFirstPoseInArray(){
lblPoseCount.text = (String(currentPoseIndex) + "/" + String(createdSession.count))
lblPoseName.text = createdSession[0].title
imgPose.image = UIImage(named: String(format: "%d.jpg", (createdSession[0].id)!))
tvDescription.text = createdSession[0].desc
}
#IBAction func btnPlaySession(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(poseDuration, target: self, selector: "getNextPoseData", userInfo: nil, repeats: true)
}
func getNextPoseData(){
if (currentPoseIndex < createdSession.count){
setProgressBar()
lblPoseCount.text = (String(currentPoseIndex + 1) + "/" + String(createdSession.count))
lblPoseName.text = createdSession[currentPoseIndex].title
imgPose.image = UIImage(named: String(format: "%d.jpg",(createdSession[currentPoseIndex].id)!))
tvDescription.text = createdSession[currentPoseIndex].desc
currentPoseIndex += 1
print(currentPoseIndex)
}
}
func setProgressBar(){
}
}
OK - so if you want the progress bar to update every second, then you need a timer that fires every second - but which does it 20 times, and calls setProgressBar as selector instead of getNextPoseData
within setProgressBar, you need to increment a class-level attribute, indexProgressBar perhaps, and simply set the progress bar attribute progress to 1.0 / indexProgressBar
if indexProgressBar == 20, then call getNextPoseData, and reset your progress bar
and here's a simplified version of how you might do that
class ViewController: UIViewController
{
#IBOutlet weak var progressBar: UIProgressView!
var timer = NSTimer!()
var poseDuration = 20
var indexProgressBar = 0
var currentPoseIndex = 0
override func viewDidLoad()
{
super.viewDidLoad()
// initialise the display
progressBar.progress = 0.0
}
#IBAction func cmdGo(sender: AnyObject)
{
// display the first pose
getNextPoseData()
// start the timer
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "setProgressBar", userInfo: nil, repeats: true)
}
func getNextPoseData()
{
// do next pose stuff
currentPoseIndex += 1
print(currentPoseIndex)
}
func setProgressBar()
{
if indexProgressBar == poseDuration
{
getNextPoseData()
// reset the progress counter
indexProgressBar = 0
}
// update the display
// use poseDuration - 1 so that you display 20 steps of the the progress bar, from 0...19
progressBar.progress = Float(indexProgressBar) / Float(poseDuration - 1)
// increment the counter
indexProgressBar += 1
}
}
For Continues loader
func setProgress() {
time += 0.1
ProgressBar.setProgress(time / 3, animated: true)
if time >= 3 {
self.time = 0.01
ProgressBar.progress = 0
let color = self.downloadProgressBar.progressTintColor
self.downloadProgressBar.progressTintColor = self.downloadProgressBar.trackTintColor
self.downloadProgressBar.trackTintColor = color
}

Timer Not Stopping When Supposed To Swift 2

I have coded my timer with the idea in mind that when my timer reaches 10, it stops. But for some reason, it doesn't.
import Foundation
import UIKit
class SinglePlayer: UIViewController {
var timerCount = 0.0
#IBOutlet weak var timer: UILabel!
var timerVar = NSTimer()
func isCounting() {
timerCount += 0.1
timer.text = "\(timerCount)"
}
override func viewDidLoad() {
super.viewDidLoad()
if timerCount <= 10.0{
timerVar = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "isCounting", userInfo: nil, repeats:true)
} else {
timerVar.invalidate()
}
}
}
You need to put condition in the callback function and not on viewDidLoad which is only called once on the load.
func isCounting() {
timerCount += 0.1
timer.text = "\(timerCount)"
if timerCount >= 10.0 {
timerVar.invalidate()
}
}

How to loop NSTimer in SWIFT with a specific number of counting

I am trying to loop a timer with a specific number of counting...
eg... 1, 2, 3, 4, 5
then loop again, the same counting, 10 times, then stop.
I was able to do the counting, but I can't do the looping of the counting.
What am I doing wrong?
var times = 0
var stopCounting = 10
#IBAction func buttonPressed(sender: UIButton) {
self.startTimer()
}
func startTimer(){
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("startCounting"), userInfo: nil, repeats: true)
}
func startCounting (){
times += 1
if times < stopCounting + 1{
if counter > -1 && counter < 6 {
counting.text = String(counter++)
} else if counter == Int(counting.text) {
counting.text = "0"
}
}
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var strTime: UILabel!
var timer = NSTimer()
var endTime: NSTimeInterval = 0
var now: NSTimeInterval { return NSDate().timeIntervalSinceReferenceDate }
var times = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func updateText(){
let time = Int(ceil(endTime - now))
if time == 0 {
times++
if times == 10 {
strTime.text = "end"
timer.invalidate()
}
endTime = NSDate().timeIntervalSinceReferenceDate + 5
} else {
strTime.text = time.description
}
}
#IBAction func buttonPressed(sender: UIButton) {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateText", userInfo: nil, repeats: true)
endTime = NSDate().timeIntervalSinceReferenceDate + 5
sender.hidden = true
}
}
to make it count from 1 to 5 you need to change from ceil to floor, abs() and add +1
func updateText(){
let time = Int(floor(abs(endTime - now - 5)))
if time == 5 {
times++
if times == 10 {
strTime.text = "10" + " " + "0"
timer.invalidate()
}
endTime = NSDate().timeIntervalSinceReferenceDate + 5
} else {
strTime.text = times.description + " " + (time+1).description
}
}
Never done swift, but took a stab in the dark, why not.
var loops = 5 // Number of times to loop the counting
var countTo = 10 // Number to count to
var loopCount = 0 // Number of loops
#IBAction func buttonPressed(sender: UIButton) {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
target: self,
selector: Selector("startCounting"),
userInfo: nil,
repeats: true)
}
func startCounting (){
// Loop from 1 to countTo, setting the text for each number
for i in 1..countTo {
counting.text = String(i)
}
// Done counting, reset to zero
counting.text = "0"
// Update the number of times the counting has run
loopCount++
// If have completed all the loops, invalidate the timer
if (loopCounter >= loops) {
self.timer.invalidate()
}
}