Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression pattern of type 'String' cannot match values of type 'AVMetadataKey'

Tags:

swift4

I'm trying to convert my Swift 3 code to Swift 4. I get this error message:

Expression pattern of type 'String' cannot match values of type 'AVMetadataKey'

private extension JukeboxItem.Meta {
mutating func process(metaItem item: AVMetadataItem) {

    switch item.commonKey
    {
    case "title"? :
        title = item.value as? String
    case "albumName"? :
        album = item.value as? String
    case "artist"? :
        artist = item.value as? String
    case "artwork"? :
        processArtwork(fromMetadataItem : item)
    default :
        break
    }
}
like image 200
Alessio Frugoni Avatar asked Dec 08 '22 17:12

Alessio Frugoni


1 Answers

Please ⌘-click on commonKey and you will see that the argument is of type AVMetadataKey rather than String.

You are encouraged to read the documentation. It's worth it and you can fix yourself an issue like this in seconds.

I added a guard statement to exit the method immediately if commonKey is nil.

private extension JukeboxItem.Meta {
    func process(metaItem item: AVMetadataItem) {

        guard let commonKey = item.commonKey else { return }
        switch commonKey 
        {
        case .commonKeyTitle :
            title = item.value as? String
        case .commonKeyAlbumName :
            album = item.value as? String
        case .commonKeyArtist :
            artist = item.value as? String
        case .commonKeyArtwork :
            processArtwork(fromMetadataItem : item)
        default :
            break
        }
    }
}
like image 125
vadian Avatar answered Apr 17 '23 04:04

vadian