bbkrz bbkrz - 6 months ago 16
Swift Question

binary operator '==' cannot be applied to operands of type 'String!' and '[String]' in Swift 2 Xcode 7

I have developed the following code and it's working fine:

if metadataObj.type == AVMetadataObjectTypeQRCode {
//the code...
}


Now, I would like for
metadataObj.type
to be equal to an array of string like the following:

let barCodeTypes = [AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode
]

if metadataObj.type == barcodeTypes {
//The code...
}


I get the following error when I use the new code:


binary operator '==' cannot be applied to operands of type 'String!' and '[String]'


Any suggestions? Thanks in advance

Answer

You can't compare string and array of strings, but you can use contains:

 if barcodeTypes.contains(metadataObj.type)
Comments