0
votes

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)
        }


    }
}
1
the solution is already given but anyway it's strange that this code compiles. You should see this compile error 'Type 'SkippingViewController' has no member 'updateTimer'' - Blazej SLEBODA

1 Answers

2
votes

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)
}