Search code examples
swiftcmtime

CMTIME_COMPARE_INLINE in Swift 3?


Want to compare CMTime and based on that I am performing loop. Converting ObjectiveC to Swift, I can't found suitable method for CMTIME_COMPARE_INLINE.

CMTime oneSecond = CMTimeMake( 1, 1 );
CMTime oneSecondAgo = CMTimeSubtract( timestamp, oneSecond );

while( CMTIME_COMPARE_INLINE( [_previousSecondTimestamps[0] CMTimeValue], <, oneSecondAgo ) ) {
    [_previousSecondTimestamps removeObjectAtIndex:0];
}

Has anybody face this similar issue in any version of Swift?


Solution

  • CMTime is imported as a Comparable type in Swift 3, so, you have no need to use CMTIME_COMPARE_INLINE.

    Assuming _previousSecondTimestamps is of type [CMTime] (*1), you can write something like this in Swift 3:

        let oneSecond = CMTime(seconds: 1.0, preferredTimescale: 1)
        let oneSecondAgo = timestamp - oneSecond
    
        while !_previousSecondTimestamps.isEmpty && _previousSecondTimestamps[0] < oneSecondAgo {
            _previousSecondTimestamps.remove(at: 0)
        }
    

    *1 In Swift, you can declare an Array of CMTime directly, you have no need to use NSMutableArray containing NSValue.