从 Swift 3.0 到 Swift 4.2 "Cannot assign value of type '[String]' to type '[AVMetadataObject.ObjectType]?'" 的转换错误

Conversion errors from Swift 3.0 to Swift 4.2 "Cannot assign value of type '[String]' to type '[AVMetadataObject.ObjectType]?'"

我是 Swift/iOS 开发的新手,正在致力于将 Swift 3.0 项目转换为 Swift 4.2 以支持更新的设备。

在此过程中出现以下错误:

Cannot assign value of type '[String]' to type '[AVMetadataObject.ObjectType]?'

下面是抛出错误的代码行:

let metadataOutput = AVCaptureMetadataOutput()

metadataOutput.metadataObjectTypes = [convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.upce),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.code39),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.code39Mod43),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.ean13),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.ean8),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.code93),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.code128),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.pdf417),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.aztec),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.interleaved2of5),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.itf14),
                                      convertFromAVMetadataObjectObjectType(AVMetadataObject.ObjectType.dataMatrix)]// Support for AVMetadataObjectTypeQRCode / qr code is removed from the list.

迁移器尽力提供帮助,但有时帮助会产生误导。

您可以只用静态成员填充数组

metadataOutput.metadataObjectTypes = [.upce, .code39, .code39Mod43, .ean13, .ean8, .code93, .code128, .pdf417, .aztec, .interleaved2of5, .itf14, .dataMatrix]