I keep getting errors in my code, specifically "Type 'FirstViewController' has no member" and "Use of unresolved identifier"

时间:2019-04-16 23:56:30

标签: ios swift timer

I get errors saying, "Type 'FirstViewController' has no member 'keepTimer'" and "Use of unresolved identifier 'keepTimer'".

What am I doing wrong? How should I change this? My main goal is to have a stopwatch start keeping track of time. When I press Save, it should add the stopwatch value as an event in Calendar.

I have made sure all are identical. They are identical in terms of spelling, but some have () at the end. When I add () at the end, I still get errors.

@IBAction func startButton(_ sender: Any) {
          captureStartDateTime()
        timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(FirstViewController.self.keepTimer), userInfo: nil, repeats: true)
        keepTimer()
        startOutlet.isHidden = true
    } ...

func keepTimer() {...}

3 个答案:

答案 0 :(得分:1)

@IBAction func startButton(_ sender: Any) {
    captureStartDateTime()
    timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(keepTimer), userInfo: nil, repeats: true)
    startOutlet.isHidden = true
}

@objc func keepTimer() {

}

答案 1 :(得分:0)

在Swift中,要在#selector()中使用方法,该方法必须标记为@objc

在您的情况下,您应该会收到一条错误消息,说Argument of '#selector' refers to instance method 'keepTimer()' that is not exposed to Objective-C 您所要做的就是添加@objc,以将该实例方法公开给Objective-C。

即您的代码应类似于@objc func keepTimer() {...}

答案 2 :(得分:-1)

Is the keepTimer() in the FirstViewController? If not, the Xcode may cannot recognise the function