Friso Stolk
Friso Stolk

Reputation: 81

Thread 1: signal SIGABRT, unrecognized selector sent to instance

I am trying to build a countdown timer in xcode but for some reason it does not work.

I have tried all kinds of methonds but I still keep getting the same error.

import Foundation
import UIKit

class SecondViewController: UIViewController {

        // text field counter
    var count:Int = 300

    @IBOutlet weak var counter: UILabel!
    let timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countDown), userInfo: nil, repeats: true)


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view
    }
    @objc func countDown(){
        counter.text = String(count)
        count -= 1


    }


}

This is the error that I got:

-[__SwiftValue countDown]: unrecognized selector sent to instance 0x6000025dde00

Upvotes: 2

Views: 1253

Answers (2)

jups
jups

Reputation: 39

just check if @IBOutlet weak var counter: UILabel! is connected to your storyboard label enter image description here

Upvotes: 0

Kamran
Kamran

Reputation: 15258

You are passing an instance method in the initialization of the instance member without using lazy computed initialization so it is not guaranteed you will get completely initialized instance to bind its method. Schedule timer as below,

class SecondViewController: UIViewController {

    // text field counter
    var count:Int = 300

    @IBOutlet weak var counter: UILabel!
    var timer: Timer?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countDown), userInfo: nil, repeats: true)
    }

    @objc func countDown(){
        counter.text = String(count)
        count -= 1
    }
}

Upvotes: 1

Related Questions