Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if CMTime is valid in Swift?

Tags:

swift

In Obj-C I could use the preprocessor macro CMTIME_IS_VALID for this.

In Swift, preprocessor macros don't exist, so I can't use this. Any other easy way to do this check? Of course, I could rewrite the definition of the macro below, but isn't there any better way to do this?

 #define CMTIME_IS_VALID(time) ((Boolean)(((time).flags & kCMTimeFlags_Valid) != 0))
like image 713
Anton Avatar asked Jun 17 '14 11:06

Anton


1 Answers

You can define a custom extension with a read-only computed property isValid:

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

which is then used as

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

Update: As of Swift 2 / Xcode 7, CMTIME_IS_VALID is imported into Swift as

func CMTIME_IS_VALID(time: CMTime) -> Bool

therefore a custom extension is not needed anymore. If you want to define a isValid property then the syntax in Swift 2 would be

extension CMTime {
    var isValid : Bool { return flags.contains(.Valid) }
}
like image 153
Martin R Avatar answered Sep 20 '22 14:09

Martin R