How to check if CMTime is valid in Swift?

In Obj-C, you can use the CMTIME_IS_VALID preprocessor macro for this.

In Swift, preprocessor macros do not exist, so I cannot use this. Any other easy way to do this check? Of course, I could rewrite the definition of the macro below, but is there a better way to do this?

  #define CMTIME_IS_VALID(time) ((Boolean)(((time).flags & kCMTimeFlags_Valid) != 0)) 
+6
source share
1 answer

You can define a custom extension with a computed value for reading isValid :

 extension CMTime { var isValid : Bool { return (flags & .Valid) != nil } } 

which is then used as

 let cm:CMTime = ... if cm.isValid { ... } 

Update:. When using Swift 2 / Xcode 7, CMTIME_IS_VALID imported into Swift as

 func CMTIME_IS_VALID(time: CMTime) -> Bool 

therefore, custom extension is no longer required. If you want to define the isValid property, then the syntax in Swift 2 will be

 extension CMTime { var isValid : Bool { return flags.contains(.Valid) } } 
+15
source

Source: https://habr.com/ru/post/970945/


All Articles