简体   繁体   English

Xcode无法识别Timer()-Swift 3

[英]Xcode doesn't Recognise Timer()- Swift 3

New to Swift here- I'm trying to achieve a simple segue operation after predefined times lapses. Swift的新功能-我正在尝试在预定义的时间过去后实现简单的segue操作。 But for some reason my Xcode isn't recognizing the Timer and gives error "Timer module has no member named scheduledTimer". 但是由于某种原因,我的Xcode无法识别Timer并给出错误“ Timer模块没有名为ScheduledTimer的成员”。 I couldn't find help anywhere. 我在任何地方都找不到帮助。

CODE: 码:

import UIKit

class ViewController: UIViewController {

    let emptystring = String()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var time = Timer.scheduledTimer(timeInterval: 8.0, target: self, selector: #selector(changeview), userInfo: nil, repeats: false)
    }

    func changeview (){
        self.performSegueWithIdentifier("GoToMain", sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "GoToMain"){
            let destination = (segue.destinationViewController as! UINavigationController).viewControllers[0] as! SecondView
            destination.emptyString = emptystring
        }
        print("Segue Performed")
    }

}

Picture also shows my entire code, including the error. 图片还显示了我的整个代码,包括错误。 NOTE: It's not my own code. 注意:这不是我自己的代码。 I only followed the answer to question in following link: 我只在以下链接中关注问题的答案:

Xcode Swift 3: Timer and Segue View Controller Error Xcode Swift 3:计时器和Segue视图控制器错误

You have declared the first parameter as timerInterval but it should be timeInterval instead. 您已将第一个参数声明为timerInterval但应改为timeInterval

So change: 所以改变:

var time = Timer.scheduledTimer(timerInterval: 8.0, target: self, selecter: #selector(changeview), userInfo: nil, repeats: false

To: 至:

var time = Timer.scheduledTimer(timeInterval: 8.0, target: self, selector: #selector(changeview), userInfo: nil, repeats: false)

Timer is a Swift 3 type, but judging from the rest of your method signatures, you appear to be using Swift 2. Use NSTimer in Swift 2. Timer是一种Swift 3类型,但是从其他方法签名来看,您似乎正在使用NSTimer在Swift 2中使用NSTimer


Also, for future reference, in addition to the timeInterval typo (which you've now fixed), the third parameter is selector , not selecter . 另外,为了供将来参考,除了timeInterval错字(您现在已修复)之外,第三个参数是selector ,而不是selecter


So, in Swift 2: 因此,在Swift 2中:

NSTimer.scheduledTimerWithTimeInterval(8.0, target: self, selector: #selector(changeview), userInfo: nil, repeats: false)

Or in Swift 3: 或在Swift 3中:

Timer.scheduledTimer(timeInterval: 8.0, target: self, selector: #selector(changeview), userInfo: nil, repeats: false)

Also note that with Swift 4, you have to add @objc to the selector you call after the timer expires, because the code that calls this selector is apparently still written in Objective-C. 还要注意,在Swift 4中,必须在计时器到期后将@objc添加到要调用的选择器中,因为调用该选择器的代码显然仍是用Objective-C编写的。

So change: 所以改变:

func changeview()

to this: 对此:

@objc func changeview()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM