-2

I'm trying to create a simple stopwatch app where the timer label increments when the startButton button is pressed. This is what I have:

    @IBOutlet weak var timer: UILabel!

    @IBAction func startButton(sender: AnyObject) {

        timer = NSTimer.scheduledTimerWithTimeInterval(0.0025, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

    }


var count = 0

func result() {
    count++
    timer.text=String(count)
    }

I get the error "Extra argument 'selector' in call" but can't workout the syntax to do it properly.

ZhouW
  • 1,187
  • 3
  • 16
  • 39
  • possible duplicate of [Using an NSTimer in Swift](http://stackoverflow.com/questions/24369602/using-an-nstimer-in-swift) – Abizern Dec 28 '14 at 11:22

1 Answers1

2

Swift error messages are a little lacking at times. It should have said something like "NSTimer is not convertible to UILabel". You are assigning the timer you create to your IBOutlet timer which is a UILabel. The timer is an NSTimer. Just assign it to a different variable when you create it and all will be fine.

@IBAction func startButton(sender: AnyObject) {

    let myTimer = NSTimer.scheduledTimerWithTimeInterval(0.0025, target: self, selector: "result", userInfo: nil, repeats: true)

}

As a shortcut, you can just use a string as a selector, so Selector("result") can be replaced by just "result".

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • That is very nice of you and I agree totally. Still, the question and answer are now nearly 2 years old, @ZhouW could have done it by now. But also, by looking at her/his stats, I assume s/he is still not a very active user, so all is forgiven. I was maybe just channeling my anger over this question, where I answered, the OP said the answer was correct in a comment and did never accept... :( http://stackoverflow.com/questions/31640075/array-index-out-of-range-error-when-optional-unbinding/31641010#31641010 – Jan Nash Oct 11 '16 at 07:50