如何在Swift 4中使用Swift 3的已废弃语法

时间:2018-02-05 08:56:40

标签: ios swift xcode avcapture

录制视频,同时设置视频编解码器如下:

sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecTypeJPEG]

XCode说'AVVideoCodecTypeJPEG' has been renamed to 'AVVideoCodecType.jpeg''AVVideoCodecTypeJPEG' was obsoleted in Swift 3,它建议Replace 'AVVideoCodecTypeJPEG' with 'AVVideoCodecType.jpeg'

完成后,XCode说'jpeg' is only available on iOS 11.0 or newer

问题是我必须使用iOS 10并且想要使用Swift 4.

在iOS 10的Swift 4中是否有使用此类功能的解决方案?

1 个答案:

答案 0 :(得分:11)

我认为解决此类问题的正确方法是使用新的AVVideoCodecType.jpeg和弃用的AVVideoCodecJPEG,这样做:

if #available(iOS 11.0, *) {
    sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecType.jpeg]
} else {
    sessionOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
}