从昨天开始我虽然理解如何在Swift中编写扩展,但现在我并不确定。我有CMTimebase类的简单扩展。
extension CMTimebase {
class func instance(withMasterClock masterClock: CMClock) -> CMTimebase {
var timebase: CMTimebase? = nil
CMTimebaseCreateWithMasterClock(kCFAllocatorDefault, masterClock, &timebase)
return timebase!
}
var rate: Float64 {
set {
CMTimebaseSetRate(self, rate)
print(self)
}
get {
return CMTimebaseGetRate(self)
}
}
var seconds: Float64 {
return CMTimeGetSeconds(CMTimebaseGetTime(self))
}
}
在我的代码中,我这样使用它:
let timebase = CMTimebase.instance(withMasterClock:captureSession.masterClock)
timebase.rate = 1.0
我想知道为什么这不起作用,因为当我打印(时基)时,调用后将速率设置为0.0。当我调用时真正有趣的是:
CMTimebaseSetRate(timebase, 1.0)
然后按预期将速率设置为1.0。我认为问题不仅与CoreMedia有关,或者我不了解扩展背后的一些基本概念。如果您知道如何更正我的扩展程序,请提供帮助。我试图瞄准Swift 3。
答案 0 :(得分:1)
确定。这是我身边的愚蠢错误。我需要休息。扩展中有一个拼写错误。它应该是而不是rate,newValue。
var rate: Float64 {
set {
// CMTimebaseSetRate(self, rate)
// Line below is correct
CMTimebaseSetRate(self, newValue)
}
get {
return CMTimebaseGetRate(self)
}
}