Skip to content

Instantly share code, notes, and snippets.

@joshbernfeld
Created January 11, 2018 05:07
Show Gist options
  • Save joshbernfeld/32e424a3799131f596cdc145b9e49e39 to your computer and use it in GitHub Desktop.
Save joshbernfeld/32e424a3799131f596cdc145b9e49e39 to your computer and use it in GitHub Desktop.
Swift encoding/decoding support for CMTime
extension CMTime: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.value = try container.decode(CMTimeValue.self, forKey: .value)
self.timescale = try container.decode(CMTimeScale.self, forKey: .timescale)
self.flags = CMTimeFlags(rawValue: try container.decode(UInt32.self, forKey: .flags))
self.epoch = try container.decode(CMTimeEpoch.self, forKey: .epoch)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.value, forKey: .value)
try container.encode(self.timescale, forKey: .timescale)
try container.encode(self.flags.rawValue, forKey: .flags)
try container.encode(self.epoch, forKey: .epoch)
}
private enum CodingKeys: String, CodingKey {
case value
case timescale
case flags
case epoch
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment