xcode - 使用scheduledTimerWithTimeInterval的NSDate错误: Extra argument 'selector' in call

标签 xcode swift nstimer

我怎么也想不通为什么 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 而不是 TRUETRUE 未在 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/

相关文章:

swift - 如何在 Swift 4 中修复 'unrecognized selector sent to instance'

iOS @property 什么时候应该在 .h 中,什么时候在 .m 中

ios - 从 AppDelegate 通知 View Controller 的正确方法是什么?

ios - POST 请求后 self.present 不起作用

ios - UISegmentedControl 边框/高亮状态

ios - iPhone在状态栏顶部添加黑色空间

ios - WKWebview Swift 4 UIProgressBar 不工作 Xcode 9

ios - Swift 对包含计时器的方法进行单元测试

iphone - NSDate 倒计时天数小时分钟数的问题

ios - 单例 SharedInstance 函数被调用两次