7

I'm trying to developing an app that includes a simple Stopwatch feature. I'm using Xcode 6 and the Swift language. Here is the code in FirstViewController

@IBAction func Stopwatch (Sender:UIButton) { 

var startTime = NSTimeInterval()

    func updateTime(timer:NSTimer) {

        //Find Current TIme

        var currentTime = NSDate.timeIntervalSinceReferenceDate()

        //Find the difference between current time and start time

        var elapsedTime :NSTimeInterval = currentTime - startTime

        //calculate the minutes in elapsed time.
        let minutes = UInt(elapsedTime / 60.0)
        elapsedTime -= (NSTimeInterval(minutes) * 60)

        //calculate the seconds in elapsed time.
        let seconds = UInt(elapsedTime)
        elapsedTime -= NSTimeInterval(seconds)

        //find out the fraction of milliseconds to be displayed.
        let hours = UInt(elapsedTime * 100)

        let strMinutes = minutes > 9 ? String(minutes):String(minutes)
        let strSeconds = seconds > 9 ? String(seconds):String(seconds)
        let strHours = hours > 9 ? String(hours):String(hours)

        //assign text to Label
        elapsedTimeLabel.text = "You have slept for \(strHours)"
    }

    //Timer 
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("updateTime:"), userInfo: nil, repeats: true)
    startTime = NSDate.timeIntervalSinceReferenceDate()

}
}

Every time I run the app (within iOS Simulator), the app crashes when I press the button (execute the Stopwatch function). The error always reads

2014-11-23 11:44:53.617 Sleep[9630:644637] -[Sleep.FirstViewController updateTime:]: 
unrecognized selector sent to instance 0x7ff6d9676e80
2014-11-23 11:44:53.622 Sleep[9630:644637] *** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-[Sleep.FirstViewController updateTime:]: unrecognized 
selector sent to instance 0x7ff6d9676e80'
*** First throw call stack:

I have no idea why this is happening and I'm pretty new to iOS programming, so please help!

That_one_guy
  • 71
  • 1
  • 2

3 Answers3

14

let updateTime be a class method .
And if it's in a pure Swift class you'll need to preface that method's declaration with @objc like:

class A {
     @objc func updateTime(timer:NSTimer) {

     }
}
mqshen
  • 501
  • 3
  • 11
  • 2
    The @objc flag doesn't appear to be needed, at least as of Swift 1.2. However, I ran into the same error because my selector signature was wrong. It needs to be setup to receive an NSTimer. e.g. `func mySelector(timer: NSTimer)` – Mason G. Zhwiti Sep 03 '15 at 22:19
0

You have defined the updateTime as a local function to Stopwatch, so it is not available outside of its scope. You should move it at class level (same as Stopwatch) in order to be accessible.

I notice however that there are some local variables which should probably be instance properties.

Antonio
  • 71,651
  • 11
  • 148
  • 165
0

Another possibility: make sure your selector function doesn't throw, as that will also give you an unrecognized selector exception:

func mySelector(timer: NSTimer) throws {
}
weltan
  • 83
  • 5