我正在尝试像这样获得输出:
1
(然后延迟一秒)
你好
2
(然后延迟一秒)
你好
3
(然后延迟一秒)
你好
但我得到的是
1
2
3
(然后延迟一秒)
你好
你好
你好
这是我调用 NSTimer 的 for 循环
var timer = NSTimer()
for i in 1...3 {
print("\(i)");
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(MainVPScreenViewController.printTest), userInfo: nil, repeats: false)
}
这是选择器方法:
func printTest() {
print("Hello")
}
预先感谢您提供的任何帮助
最佳答案
在没有 NSTimer 的情况下尝试这个解决方案:
var i = 1
func printHello() {
print(i)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
print("Hello")
i +=1
if i <= 3 {
printHello()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
printHello()
}
关于ios - Swift 2 - 定时 Action 间隔一秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38086104/