"String"类型的表达式模式不能匹配"AVMetadataKey"类型的值

Ale*_*oni 3 swift4

我正在尝试将我的Swift 3代码转换为Swift 4.我收到此错误消息:

"String"类型的表达式模式不能匹配"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
    }
}
Run Code Online (Sandbox Code Playgroud)

vad*_*ian 11

⌘-click打开commonKey,你会看到论证的类型AVMetadataKey而不是String.

我们鼓励您阅读文档.这是值得的,你可以在几秒钟内解决这个问题.

我添加了一个guard声明,如果commonKey是,立即退出该方法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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)