Search code examples
swiftnsnumbernstimeinterval

Swift- Cannot invoke '-' with an argument list of type (NSNumber , NSTimeinterval)


i am trying to get the current time passes from certain point from the starting point of the app.

var startTime = NSDate.timeIntervalSinceReferenceDate()
     var currentTime: NSNumber = 0

    for var i = 0; i < 10000; i++ {
        if i == 99 {
            currentTime = NSDate.timeIntervalSinceReferenceDate()
            println(i)
        }

    }
    var elapsedTime = currentTime - startTime
    println(Double(elapsedTime))
}

and i get this error :

Cannot invoke '-' with an argument list of type '(@!Value NSNumber, @!Value NSTimerinerval)

i understand it's something with the type i jutt cant figure it out, any suggestions?


Solution

  • startTime is a NSTimeInterval, which is not the same thing as NSNumber, which is what you declared currentTime to be at the top of that function.

    Change your declaration of currentTime to:

     var currentTime: NSTimeInterval = 0