Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何等待给定的时间运行操作_Ios_Swift - Fatal编程技术网

Ios 如何等待给定的时间运行操作

Ios 如何等待给定的时间运行操作,ios,swift,Ios,Swift,我想知道在使控制台计数给定时间后如何运行操作。例如,在3秒钟后打印“hello”。 谢谢 您需要使用NSTimer类 用法示例: var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("sayHello"), userInfo: nil, repeats: true) 在你们班的其他地方 func sayHello() { println("Hello") } 您可

我想知道在使控制台计数给定时间后如何运行操作。例如,在3秒钟后打印“hello”。
谢谢

您需要使用
NSTimer

用法示例:

var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("sayHello"), userInfo: nil, repeats: true)
在你们班的其他地方

func sayHello() {
    println("Hello")
}

您可以通过以下方式创建延迟!! 您可以使用
asynAfter

DispatchQueue.main.asyncAfter(deadline: .now() + <secondsToDelay>) {
   // Put your code here that you want to execute after delay
}

当然,你也可以像伍德斯托克的回答一样使用
定时器。

你考虑过并尝试过什么?谢谢你的帮助:)@ManewL很乐意帮忙。
perform(#selector(delayedMethod), with: nil, afterDelay: <secondsToDelay>)

@objc func delayedMethod() {
    print("My delayed method call")
}