bbkrz
bbkrz

Reputation: 433

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

Upvotes: 2

Views: 8951

Answers (2)

MirekE
MirekE

Reputation: 11555

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

 if barcodeTypes.contains(metadataObj.type)

Upvotes: 3

Jared
Jared

Reputation: 588

It looks like you are trying to check if any strings in your barCodeTypes array match metadataObj.type. You want a for in loop.

for barCode in barCodeTypes {
    if metadataObj.type == barCode {
       //the code...
    }
}

This iterates over every item in barCodeTypes and checks if they are equal.

Upvotes: -1

Related Questions