如何通过Swift NSTimer调用其选择器来修复错误 [英] How to Fix Error with a Swift NSTimer Calling Its Selector
问题描述
我遇到了运行时错误:
2014-07-15 16:49:44.893 TransporterGUI [1527:303]-[_ TtC14TransporterGUI11AppDelegate printCountdown]:无法识别的选择器已发送到实例0x10040e8a0
2014-07-15 16:49:44.893 TransporterGUI[1527:303] -[_TtC14TransporterGUI11AppDelegate printCountdown]: unrecognized selector sent to instance 0x10040e8a0
当我使用以下Swift代码触发计时器时:
when I use the following Swift code to fire a timer:
@IBAction func schedule(sender : AnyObject) {
var startTime = startDatePicker.dateValue.timeIntervalSinceDate(NSDate())
var endTime = endDatePicker.dateValue.timeIntervalSinceDate(startDatePicker.dateValue)
var startDate = NSDate.date()
let params = ["startTime": startTime, "startDate": startDate]
var counter = NSTimer.scheduledTimerWithTimeInterval(1.0, target:self, selector:Selector("printCountdown"),
userInfo:params, repeats:true)
}
func printCountdown(timer: NSTimer) {
var userInfo = timer.userInfo as NSDictionary
var startTime = userInfo["startTime"] as NSTimeInterval
var startDate = userInfo["startDate"] as NSDate
var elapsedTime: NSTimeInterval = NSDate.date().timeIntervalSinceDate(startDate)
var remainingTime: NSTimeInterval = startTime - elapsedTime;
if (remainingTime <= 0.0) {
timer.invalidate()
transferLabel.title = "No transfer scheduled"
}
transferLabel.title = remainingTime.description
}
奇怪的是,如果我将函数printCountdown的签名更改为不带参数,则将适当地调用该函数,但是我无法访问进行该调用的计时器对象.
Oddly enough, if I change the signature of the function printCountdown to have no parameters, the function is called appropriately, but then I have no way of accessing the timer object that made the call.
提前谢谢!
推荐答案
您的选择器应为"printCountdown:",并以冒号结尾,以指示选择器接受参数.
Your selector should be "printCountdown:", with a terminating colon to indicate that the selector takes a parameter.
这篇关于如何通过Swift NSTimer调用其选择器来修复错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!