gpt4 book ai didi

ios - Swift:我可以加减 `dispatch_time_t` 变量吗?

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:40 26 4
gpt4 key购买 nike

我需要在 iOS 中使用 Swift 做一些时间计算。

我必须使用 dispatch_walltime。我希望这可以被视为公理化。在涉及时间数学的地方,我想我可能会得到“只需使用 NSDate”的响应,但请相信它:我必须遵守 dispatch_walltime

现在,很明显为什么有人会建议使用 NSDate,因为当您使用 NSTimeIntervalNSDate 以及那些好东西时,它非常棒轻松制作自定义时间戳并比较它们并进行各种时间数学计算。

但我必须使用 dispatch_time_t,特别是像这样创建的 dispatch_walltime:

//Get the timeInterval of now.
let nowInterval = NSDate().timeIntervalSince1970

//Make a timespec from it.
var nowStruct = timespec(tv_sec: Int(nowInterval), tv_nsec: 0)

//Make a walltime definition from that.
let referenceWalltime = dispatch_walltime(&nowStruct, 0)

稍后我需要以各种方式使用该引用时间。例如,我需要获取引用时间和它恰好是的任何时间之间的时间间隔。

我正在尝试以与 NSTimeInterval 相同的方式执行此操作,换句话说,创建一个新的并从中减去旧的:

//Repeat everything from before to make a new wall time.
let newNowInterval = NSDate().timeIntervalSince1970
var newNowStruct = timespec(tv_sec: Int(newNowInterval), tv_nsec: 0)
let newWalltime = dispatch_walltime(& newNowStruct, 0)

//Time math a la NSTimeInterval to find the interval:
let walltimeInterval = newWalltime - referenceWalltime

那行得通吗?

最佳答案

简短的回答是:不。该代码将崩溃。

更好的答案是:不,但可以做到,而且最终并没有什么不同。

我自己在 Playground 进行了一些调查并学到了一些有趣的东西,我相信我找到了正确的方法。

我将我的整个 Playground 粘贴到这里,以便其他人可以复制粘贴它并弄清楚如何进行他们自己的 dispatch_time 数学运算。

代码中标有//********* 的注释表示我学到的关键内容。

import UIKit
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

public class IntervalMaker {

var referenceWalltime: dispatch_time_t = 0
var newWalltime: dispatch_time_t = 0
var walltimeInterval: dispatch_time_t = 0

func scheduleWalltimeSequence () {
let threeSeconds = Int64(NSEC_PER_SEC) * 3
let now = walltimeNow()
let dispatchTimeInThree = dispatch_time(now, threeSeconds)
let dispatchTimeInSix = dispatch_time(now,
2 * threeSeconds)
setReferenceWalltimeToNow()
dispatch_after(dispatchTimeInThree, dispatch_get_main_queue(),
setNewWalltimeToNow)
dispatch_after(dispatchTimeInSix,
dispatch_get_main_queue(), dispatchBasedOnDispatchMath)
}

func walltimeNow()->dispatch_time_t{
let nowInterval = NSDate().timeIntervalSince1970
var nowStruct = timespec(tv_sec: Int(nowInterval), tv_nsec: 0)
return dispatch_walltime(&nowStruct, 0)
}

func setReferenceWalltimeToNow () {
referenceWalltime = walltimeNow()
}

func setNewWalltimeToNow (){
newWalltime = walltimeNow()
}

func dispatchBasedOnDispatchMath () {
computeInterval() //Should be three seconds
let nineTheWrongWay = referenceWalltime + (walltimeInterval * 3)
let nineTheRightWay = dispatch_time(referenceWalltime,
Int64(walltimeInterval) * 3)
dispatch_after(nineTheWrongWay,
dispatch_get_main_queue(), finalPrintln)
//********** THE ABOVE DOES NOT WORK CORRECTLY - prints 6 seconds later
dispatch_after(nineTheRightWay,
dispatch_get_main_queue(), finalPrintln)
//********** THE ABOVE WORKS CORRECTLY - prints 9 seconds later
}

func finalPrintln () {
let now = walltimeNow()
println("I should be printing nine seconds from reference time.")
println("It's actually \(referenceWalltime - now) nanoseconds after")
}

func computeInterval () {
walltimeInterval = referenceWalltime - newWalltime
//********** dispatch_walltimes actually count backwards, and *CANNOT* be
//********** negative: writing `newWalltime - referenceWalltime` will crash
}
}

let intervaller = IntervalMaker()
intervaller.scheduleWalltimeSequence()

关于ios - Swift:我可以加减 `dispatch_time_t` 变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30006588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com