在swift中执行一段时间后执行代码操作

时间:2016-06-26 00:52:17

标签: ios xcode swift

我想在120秒后运行一个动作,但是为了让其余代码继续运行而不中断,有没有办法在swift中执行此操作?例如

var timer = 0
if timer == 120{
print("time up")
}
//But This code still needs to be able to be run
if buttonPressed == true{
 print("pressed")
}

1 个答案:

答案 0 :(得分:1)

请参阅https://stackoverflow.com/a/28821805/6496271

  

让triggerTime =(Int64(NSEC_PER_SEC)* 10)   dispatch_after(dispatch_time(DISPATCH_TIME_NOW,triggerTime),dispatch_get_main_queue(),{() - > Void in       self.functionToCall()   })

10秒后调用self.functionToCall()

使它成为120秒:

let triggerTime = (Int64(NSEC_PER_SEC) * 120)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in
    self.functionToCall()
})