我怎么也想不通为什么 Xcode 会抛出错误“调用中存在额外参数‘选择器’”。方法签名很好,并且没有额外的“选择器”参数。此外,自动完成功能会显示该方法,但按住 Option 键单击 ScheduledTimerWithTimeInterval 会显示“无快速帮助”,并且字体为黑色而不是紫色,就好像编译器无法识别它一样。这是代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var stopButton: UIButton!
var timer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
startButton.layer.cornerRadius = 5.0
stopButton.layer.cornerRadius = 5.0
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func didPressStartButton(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector:Selector("updateTimeLabel"), userInfo: NSDate(), repeats: TRUE)
}
func updateTimerLabel() {
}
}
最佳答案
尝试使用 true
而不是 TRUE
。 TRUE
未在 Swift 中定义,因此该调用无法编译。快速错误是神秘的 - 它通常无法得到正确的故障点。
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector:Selector("updateTimeLabel"), userInfo: NSDate(), repeats: true)
在 Playground 中运行良好。
更新
可能还值得指出的是,Selector()
“函数”是多余的 - 函数的字符串名称 - 在本例中 "updateTimeLabel"
- 同样有效.
关于xcode - 使用scheduledTimerWithTimeInterval的NSDate错误: Extra argument 'selector' in call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26102813/