Why my timer won't go?

I am a total noob and I'm learning.


I have a question: why I get an error after my application start?


This is the code for the timer:


Import UIKit


var counter = 0


class ViewController: UIViewController {
    
    
    
    
    @IBOutlet weak var Label1: UILabel
    @IBOutlet weak var Switch1: UISwitch!
        
    
    
  var  timer1 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil , repeats: true)
    
    
    @objc func timerAction() {
        counter =  counter + 1
        Label1.text = String(counter)
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
    
        
        
        // Do any additional setup after loading the view.
    }


    
    
}



Thanks a lot in advance!

What error do you get ? Where exactly ?


Some presentation remark.

In code, I prefer avoiding inserting too many blank lines, which makes it difficult to get a global view on code.


Import UIKit

var counter = 0

class ViewController: UIViewController {
    
    @IBOutlet weak var Label1: UILabel!          // There was a ! missing
    @IBOutlet weak var Switch1: UISwitch!
    
    var  timer1 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil , repeats: true)
    
    @objc func timerAction() {
        counter =  counter + 1
        Label1.text = String(counter)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
}

Line 07: ! was missing.

Did you connect the label to object in IB ?

To check it works, add line 14:

print("counter", counter)

I have a question: why I get an error after my application start?

When you ask somethng about errors, you should better show the error messages.


And I want to ask you one thing, which sort of errors do you get, compile-time error or runtime error?


Anyway, your code has two critical flaws that it cannot be built:

import UIKit //<-

var counter = 0
  
class ViewController: UIViewController {
      
    @IBOutlet weak var Label1: UILabel! //<-
    @IBOutlet weak var Switch1: UISwitch!

    //...      
}

`Import` needs to start with lowercase and type of `@IBOutlet` needs to be Optional.

I'm not sure those are only in the posted code or not, so I assume you have a buildable code.


This is the most critical line in your code:

    var  timer1 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil , repeats: true)

In Swift, you cannot use `self` as a usual meaning in the initial value of instance properties, you need to initialize such a property inside some method. For example:

import UIKit //<-

var counter = 0
  
class ViewController: UIViewController {
      
    @IBOutlet weak var Label1: UILabel! //<-
    @IBOutlet weak var Switch1: UISwitch!
      
    @objc func timerAction() {
        counter =  counter + 1
        Label1.text = String(counter)
    }
    var timer1: Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
          
        // Do any additional setup after loading the view.
        timer1 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil , repeats: true)
    }
}

And don't forget to stop the timer if you don't need anymore as it consumes resources.

Why my timer won't go?
 
 
Q