Expression pattern of type 'String' cannot

2019-07-12 18:06发布

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
    }
}

标签: swift4
1条回答
疯言疯语
2楼-- · 2019-07-12 18:44

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
        }
    }
}
查看更多
登录 后发表回答